我正在使用药物库中的XML格式数据。 我尝试解析药物名称及其机制,但我的程序不断抛出以下错误:
Traceback (most recent call last):
File "C:\Users\erttll\workspace\drugbank_parsing\main.py", line 9, in <module>
tree = parse("drugbank.xml")
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: encoding specified in XML declaration is incorrect: line 1, column 31
我不知道为什么会这样。文档的编码是UTF-8。
这是我的代码:
from xml.etree.ElementTree import parse
if __name__ == '__main__':
tree = parse("drugbank.xml")
root = tree.getroot()
for drug in root.iter("drug"):
if drug != None:
drug_name = drug.findtext("name")
parmacodynamics = drug.findtext("pharmacodynamics")
MOA = drug.findtext("mechanism-of-action")
MOA_result = open(".\MOA_result\%s.txt"%drug_name,"w")
MOA_result.write(parmacodynamics + MOA)
else:
continue
plus:XML标头就像这样
<?xml version="1.0" encoding="utf-8"?>