I am using this this tutorial to go through some concepts of Python, however, I ran into this problem trying to use this line of a code in 2.76. Dave is using 2.73 at the 53 minute part of the video in the monitor.py file.
I am using this line of code to try to fix an error...
from xml.etree.ElementTree import parse
As oppose to
import xml.etree.ElementTree import parse
(which is what is used in the video in python 2.7.3)
however it doesn't work when I use it. I get this error instead.
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 : line 50, column 47
What import line of code should I write and why? I have tried 'as' and 'import'
Here is the code that I am using.
#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)