Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用minidom和regex解析XML_Python_Xml_Regex_Unicode - Fatal编程技术网

Python 使用minidom和regex解析XML

Python 使用minidom和regex解析XML,python,xml,regex,unicode,Python,Xml,Regex,Unicode,我试图解析一些XML,寻找标记名为“ip”的元素,最终我需要一个包含ip地址的字符串列表。以下是我尝试过的: def parseHosts(xmldoc): hostsNode = xmldoc.firstChild xmlList = hostsNode.getElementsByTagName("ip") ipList = [] for ip in xmlList: ipList.append(ip.childNodes[0].nodeValue) print

我试图解析一些XML,寻找标记名为“ip”的元素,最终我需要一个包含ip地址的字符串列表。以下是我尝试过的:

def parseHosts(xmldoc):
  hostsNode = xmldoc.firstChild
  xmlList = hostsNode.getElementsByTagName("ip")

  ipList = []
  for ip in xmlList:
    ipList.append(ip.childNodes[0].nodeValue)

  print ipList
>>>[u'172.16.60.92', u'172.16.60.89', u'\n              ', u'172.16.60.90', u'172.16.60.91', u'172.16.60.93']
没关系。但我需要一个IP地址字符串列表。。。我不希望节点为空。只是一个很好的地址列表,如下所示:

['172.16.60.1', '172.16.60.5', 172.16.60.100']
我试过一点带有列表的正则表达式

  regex = re.compile(r'172\.16\.[0-9]*\.[0-9]*')
  [m.group(0) for l in ipList for m in [regex.search(1)] if m]
但是我得到了以下错误

File "myParser.py", line 47, in parseHosts
[m.group(0) for l in ipList for m in [regex.search(1)] if m]
TypeError: expected string or buffer
尽管我可能会尝试,但我无法发现ipList使用的是
type(ipList)
类型,也无法找出如何将这些内容设置为字符串

还有。。。摆脱那些Unicode的东西会很好


很明显,我在这里的某个地方走到了尽头,但我不确定该去哪里寻找。

让我们回到您的原始代码。它在
ipList
中结束:

[u'172.16.60.92', u'172.16.60.89', u'\n              ', u'172.16.60.90', u'172.16.60.91', u'172.16.60.93']
import re
ipList=[u'172.16.60.92', u'172.16.60.89', u'\n              ', u'172.16.60.90', u'172.16.60.91', u'172.16.60.93']
regex = re.compile(r'172\.16\.[0-9]*\.[0-9]*')
filter(regex.match,ipList)

out:

[u'172.16.60.92',
 u'172.16.60.89',
 u'172.16.60.90',
 u'172.16.60.91',
 u'172.16.60.93']
这里唯一的问题是,它包括充满空格的字符串,以及包含IP地址的字符串,对吗

那么,让我们在事实发生后对其进行过滤:

In [51]: ipList = [u'172.16.60.92', u'172.16.60.89', u'\n              ', u'172.16.60.90', u'172.16.60.91', u'172.16.60.93']

In [52]: ipList = [ip for ip in ipList if ip.strip()]

In [53]: ipList
Out[53]: 
['172.16.60.92',
 '172.16.60.89',
 '172.16.60.90',
 '172.16.60.91',
 '172.16.60.93']
你完了

为什么这样做有效?嗯,
ip.strip()
将删除左右两侧的所有空白。将结果粘贴到
if
语句中,如果有剩余,则结果为真;如果没有剩余,则结果为假

但显然,您可以将相同的条件移回原始循环,将其放在
append
调用之前,效果完全相同:

def parseHosts(xmldoc):
  hostsNode = xmldoc.firstChild
  xmlList = hostsNode.getElementsByTagName("ip")

  ipList = []
  for ip in xmlList:
    ipstr = ip.childNodes[0].nodeValue
    if ipstr.strip():
      ipList.append(ipstr)
但整个
ipList
部分显然只是列表理解的冗长版本,因此:

def parseHosts(xmldoc):
  hostsNode = xmldoc.firstChild
  xmlList = hostsNode.getElementsByTagName("ip")
  ipList = [ip.childNodes[0].nodeValue for ip in xmlList
            if ip.childNodes[0].nodeValue.strip()]
至于您试图修复此问题:

[m.group(0) for l in ipList for m in [regex.search(1)] if m]
当嵌套列表理解的作用不明显时,将其分为两个理解

但让我们将其重写为一个显式循环。这不仅使它更容易理解,而且使调试更容易:

result = []
for l in ipList:
    for m in [regex.search(1)]:
        if m:
            result.append(m.group(0))

运行此命令时,第三行会出现异常,原因应该很明显。

您可以使用
过滤器(regex.match,ipList)


你在哪里看到了要去掉的“uuencode东西”?还有,为什么不能使用
type(ipList)
?你希望它有什么好处?您将其创建为一个空的
列表
,然后在其上调用
append
,那么除了
列表
,它还能是什么呢?uencode内容位于ipList u的172.16.60.92'中,它不是uencoded。uuencoded文本看起来像是
“begin666\n,,3当我使用type(ipList)时,我什么也没有得到。它不会告诉我它是一个字符串还是一个列表或者其他什么。。。我知道我创建了一个列表,但是有一个类型错误。那会在哪里?太好了!非常感谢。除了我没有提到列表中的一些IP可能不遵循我的正则表达式。我想要的IP只有172.16.*.*。。。所以我想我需要那个正则表达式在那里的某个地方。@TheWellington:好的,你应该能想出如何把它加进去。只需将测试从发布问题中的简单测试更改为实际需要使用正则表达式的测试,直到使用正则表达式的部分完成为止。我仍然得到TypeError:需要字符串或缓冲区。正则表达式对列表的内容类型有影响吗?@TheWellington:什么列表?查看要传递给
regex.search的参数。(顺便说一句,这就是为什么你通常不想给变量命名
l
)@TheWellington:一旦你解决了这个问题:你为什么要在[anything]中为m命名
?它保证只循环一次,将
m
设置为
anything
。通过执行
m=anything
并删除循环,可以获得完全相同的效果。这是为了做其他事情,还是没有必要?