【问题标题】:XML Module Parsing Differences Between 2.7.6 and 2.7.3 in PythonPython 中 2.7.6 和 2.7.3 之间的 XML 模块解析差异
【发布时间】:2014-03-09 02:23:02
【问题描述】:

我正在使用这个tutorial 来了解 Python 的一些概念,但是,我在 2.76 中尝试使用这行代码时遇到了这个问题。 Dave 在 monitor.py 文件中视频的 53 分钟部分使用 2.73。

我正在使用这行代码来尝试修复错误...

from xml.etree.ElementTree import parse

反对

import xml.etree.ElementTree import parse 

(这是python 2.7.3视频中使用的)

但是当我使用它时它不起作用。我得到了这个错误。

   PS C:\python27> python monitor.py
   Traceback (most recent call last):
     File "monitor.py", line 28, in <module>
        monitor()
      File "monitor.py", line 15, in monitor
       doc = parse(u)
      File "C:\Python27\lib\xml\etree\ElementTree.py", line 1182, in parse
       tree.parse(source, parser)
      File "C:\Python27\lib\xml\etree\ElementTree.py", line 656, in parse
       parser.feed(data)
     File "C:\Python27\lib\xml\etree\ElementTree.py", line 1642, in feed
        self._raiseerror(v)
      File "C:\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror
        raise err
    xml.etree.ElementTree.ParseError: undefined entity &nbsp;: line 50, column 47

我应该写哪一行代码,为什么?我试过'as'和'import'

这是我正在使用的代码。

#monitor.py
import urllib
from xml.etree.ElementTree import parse

candidates = ['4131','4163','4132']
daves_latitude = 41.980262


def distance(lat1, lat2):
    'Return distance in miles between two lats'
    return 69*(lat1 - lat2)

def monitor():
    u = urllib.urlopen('http://ctabustracker.com/bustime/mapgetBusesForRoute.jsp?route=22')
    doc = parse(u)
    for bus in doc.findall('bus'):
        busid = bus.findtext('id')
        if busid in candidates:
            lat = float(bus.findtext('lat'))
            dis = distance(lat, daves_latitude)
            print busid, dis, 'miles'
        print '-'*10


import time

while True:
    monitor()
    time.sleep(60)

【问题讨论】:

标签: python xml python-2.7


【解决方案1】:
#XXX INCORRECT, DO NOT USE IT
import xml.etree.ElementTree import parse 

错误xml.etree.ElementTree.ParseError: undefined entity &amp;nbsp 与您以任何方式导入ElementTree 模块的方式无关。 Python 2.7.6 和 2.7.3 中的行为相同。

@karthikr linked to the question in the comments 解释了这个问题:&amp;nbsp; 默认情况下没有为 xml 定义,请参阅Parse XML with (X)HTML entities

【讨论】:

  • 当我使用它时,我在第 3 行得到一个无效的语法错误。
猜你喜欢
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多