【问题标题】:Parsing in XML in Python 2.7在 Python 2.7 中解析 XML
【发布时间】:2016-10-03 09:26:51
【问题描述】:
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

当我在 cmd 中运行上述代码时,我得到以下回溯

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 

请帮忙,我不明白为什么会这样?

【问题讨论】:

    标签: xml python-2.7 elementtree


    【解决方案1】:

    你调用了你的文件xml.py

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

    所以 python 正在寻找该文件而不是 etree.ElementTree 的内置 lib xml。因此,将文件重命名为 xml.py 以外的名称并删除目录中的所有 xml.pyc 文件

    【讨论】:

      猜你喜欢
      • 2018-01-27
      • 1970-01-01
      • 2011-08-29
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多