【问题标题】:parse Dutch NDW xml解析荷兰语 NDW xml
【发布时间】:2018-01-15 23:02:57
【问题描述】:

我正在尝试解析来自荷兰 NDW 的 XML 文件,其中包含许多荷兰高速公路上每分钟的交通速度。我使用这个示例文件:http://www.ndw.nu/downloaddocument/e838c62446e862f5b6230be485291685/Reistijden.zip

我正在尝试使用 Python 解析变量中的旅行时间数据,但我很挣扎。

from xml.etree import ElementTree
import urllib2
url = "http://weburloffile.nl/ndw/Reistijden.xml"
response = urllib2.urlopen(url)
namespaces = {
    'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
    'a': 'http://datex2.eu/schema/2/2_0'
     }
dom = ElementTree.fromstring(response.read)
names = dom.findall(
        'soap:Envelope'
        '/a:duration',
        namespaces,
)
#print names
for duration in names:
    print(duration.text)

我收到这个新错误

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    dom = ElementTree.fromstring(response.read)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
    parser.feed(text)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1651, in feed
    self._parser.Parse(data, 0)
TypeError: Parse() argument 1 must be string or read-only buffer, not instancemethod

如何正确解析这个(复杂的)xml?

-- 按照评论的建议将其更改为已读

【问题讨论】:

    标签: python performance xml-parsing traffic


    【解决方案1】:

    问题不在于 XML 解析;那是您错误地使用了response 对象。 urllib2.urlopen 返回一个没有content 属性的类文件对象。相反,您应该调用read

    dom = ElementTree.fromstring(response.read())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-10
      • 2017-07-15
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      相关资源
      最近更新 更多