【问题标题】:ElementTree - ParseError: not well-formed (invalid token)ElementTree - ParseError:格式不正确(无效标记)
【发布时间】:2014-12-14 15:00:17
【问题描述】:

我正在尝试使用 urlopen 打开一个 XML 文件并读取它。但是,我不断收到以下错误:xml.etree.ElementTree.ParseError: not well-formed (invalid token)

代码如下:

    def wx(icao):
        if re.search(r'!wx *\w', icao):
            icao = ircmsg.split('PRIVMSG')[-1].split(':!wx')[1]

            icao = icao.upper()

            link = urlopen('http://w1.weather.gov/xml/current_obs/%s.xml' % icao)
            doc = link.read()

            parser = ET.XMLParser(encoding='utf-8')
            tree = ET.fromstring(doc, parser=parser)

            station_id = tree.find('station_id').text
            location = tree.find('location').text
            observation_time = tree.find('observation_time_rfc822').text
            wind = tree.find('wind_string').text
            vis = tree.find('visibility_mi').text
            weather = tree.find('weather').text
            temp = tree.find('temperature_string').text
            dew = tree.find('dewpoint_string').text

【问题讨论】:

  • 对我有用,也许您要求的国际民航组织代码错误。
  • 我使用的是 Python 2.7.9 并且无法正常工作。我仔细检查了国际民航组织,它是有效的。
  • 在我的测试中,我删除了 reircmsg 行,所以问题可能出在这两个上。

标签: python python-2.7 xml-parsing elementtree urlopen


【解决方案1】:

我猜问题出在tree = ET.fromstring(doc, parser=parser) 这一行。将其更改为

 tree = ET.fromstring(doc)

似乎 ET.fromstring 接受一个参数。

【讨论】:

猜你喜欢
  • 2013-04-05
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 2017-05-02
  • 1970-01-01
  • 2016-05-04
  • 2018-12-05
相关资源
最近更新 更多