2016-10-03 1 views
2
import urllib 
import xml.etree.ElementTree as ET 

url = raw_input('Enter location: ') 
print 'Retrieving', url 
uh = urllib.urlopen(url) 
data = uh.read() 
print 'Retrieved',len(data),'characters' 
tree = ET.fromstring(data) 
lst = tree.findall('.//count') 
print 'Count:', len(lst) 
total = 0 
for comment in tree.findall("./comments/comment"): 
    total += int(comment.find('count').text) 
print total 

Wenn ich den obigen Code in cmd ausführen, bekomme ich die folgenden ZurückverfolgungsParsing in XML in Python 2.7

C:\Users\hp1\Downloads\python\programs>xml1.py 
Traceback (most recent call last): 
File "C:\Users\hp1\Downloads\python\programs\xml1.py", line 2, in <module> 
import xml.etree.ElementTree as ET 
File "C:\Users\hp1\Downloads\python\programs\xml.py", line 2, in <module> 
import xml.etree.ElementTree as ET 
ImportError: No module named etree.ElementTree 

Bitte helfen Sie, ich verstehe nicht, warum ist das passiert?

Antwort

1

Sie nannten die Dateien xml.py:

"C:\Users\hp1\Downloads\python\programs\xml.py" 

So Python ist in dieser Datei nicht die builtin lib xml für etree.ElementTree suchen. So benennen Sie die Datei in etwas anderes als xml.py um und entfernen Sie alle xml.pyc Dateien in dem Verzeichnis