給定一個帶有中文字符的XML,我想使用xml.etree
來幫助我解析XML來做一些處理。英文版的作品。例如:xml.etree.ElementTree for chinese
>el.xml printf '%s\n' $'<?xml version=\'1.0\' encoding=\'utf8\'?><Color>Grey</Color>'
>cl.xml printf '%s\n' $'<?xml version=\'1.0\' encoding=\'utf8\'?><Color>灰色</Color>'
tryParse() {
python -c 'import xml.etree.ElementTree as ET; import sys; ET.parse(sys.argv[1])' "[email protected]"
}
tryParse el.xml && printf '%s\n\n' "English works"
tryParse cl.xml && printf '%s\n\n' "Chinese works"
...發射作爲輸出:
English works
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1182, in parse
tree.parse(source, parser)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 656, in parse
parser.feed(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1642, in feed
self._raiseerror(v)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
raise err
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 44
你需要'ET.fromstring()','不ET.parse( )';此代碼正在尋找**文件**,其名稱中含有''。 –
...這就是說 - 在這裏給出的代碼,*如果我們修復了破損的變量名稱,然後是https://stackoverflow.com/questions/21713527/xml-parsing-from-web-響應或https://stackoverflow.com/questions/3064247/cant-parse-xml-effectively-using-python,它們共享相同的基本原因。 –
秒讓我改變代碼。 – bryansis2010