我有这个 python 脚本:
import xmltodict
with open('file.xml') as fd:
doc = xmltodict.parse(fd.read())
print("Name=" + doc['Data']['form'][0]['@Name'])
print("Time=" + doc['Data']['form'][0]['Period']['@Time'])
print("\tM-1\tM-2\tM-3\tM-4")
load_m1 = doc['Data']['form'][0]['Value'][0]['val'][0]['#text']
load_m2 = doc['Data']['form'][0]['Value'][1]['val'][0]['#text']
load_m3 = doc['Data']['form'][0]['Value'][2]['val'][0]['#text']
load_m4 = doc['Data']['form'][0]['Value'][3]['val'][0]['#text']
memory_m1 = doc['Data']['form'][0]['Value'][0]['val'][1]['#text']
memory_m2 = doc['Data']['form'][0]['Value'][1]['val'][1]['#text']
memory_m3 = doc['Data']['form'][0]['Value'][2]['val'][1]['#text']
memory_m4 = doc['Data']['form'][0]['Value'][3]['val'][1]['#text']
cpu_m1 = doc['Data']['form'][0]['Value'][0]['val'][2]['#text']
cpu_m2 = doc['Data']['form'][0]['Value'][1]['val'][2]['#text']
cpu_m3 = doc['Data']['form'][0]['Value'][2]['val'][2]['#text']
cpu_m4 = doc['Data']['form'][0]['Value'][3]['val'][2]['#text']
task_m1 = doc['Data']['form'][0]['Value'][0]['val'][3]['#text']
task_m2 = doc['Data']['form'][0]['Value'][1]['val'][3]['#text']
task_m3 = doc['Data']['form'][0]['Value'][2]['val'][3]['#text']
task_m4 = doc['Data']['form'][0]['Value'][3]['val'][3]['#text']
print("Load\t" + load_m1 + "\t" + load_m2 + "\t" + load_m3 + "\t"+ load_m4)
print("Memory\t" + memory_m1 + "\t" + memory_m2 + "\t" + memory_m3 + "\t" + memory_m4)
print("CPU\t" + cpu_m1 + "\t" + cpu_m2 + "\t" + cpu_m3 + "\t" + cpu_m4)
print("Task\t" + task_m1 + "\t" + task_m2 + "\t" + task_m3 + "\t" + task_m4 )
如果我从 python3 控制台逐行运行它没问题,但是如果我将它保存在一个文件中并运行它
$ python3 script.py
它返回此错误:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/xmltodict.py", line 5, in <module>
import defusedexpat as expat
ImportError: No module named 'defusedexpat'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "xml.py", line 1, in <module>
import xmltodict
File "/usr/lib/python3.4/site-packages/xmltodict.py", line 7, in <module>
from xml.parsers import expat
File "/home/script.py", line 3, in <module>
doc = xmltodict.parse(fd.read())
AttributeError: 'module' object has no attribute 'parse'
如果稍后我想从控制台逐行再次运行它,则会发生这种情况:
Python 3.4.3 (default, May 5 2015, 17:58:45)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmltodict
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/xmltodict.py", line 5, in <module>
import defusedexpat as expat
ImportError: No module named 'defusedexpat'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/xmltodict.py", line 7, in <module>
from xml.parsers import expat
File "/home/xml.py", line 3, in <module>
doc = xmltodict.parse(fd.read())
AttributeError: 'module' object has no attribute 'parse'
>>>
我在 Ubuntu 和 cygwin 上对此进行了测试。我是这样安装的:cygwin下的'pip3 install xmltodict'和Ubuntu下的这种方式:'sudo apt-get install python3-xmltodict'