最近、Python(Ver3.7)を始めました。
Pythonで、日本語を使ったXMLファイルを読み込めません。
XMLファイルの先頭1行目に、encoding="Shift_JIS" を記述しているとPython側で読み込むときにエラーが発生します。
どのような対処をしたら良いか教えてください。
発生している問題・エラーメッセージ
python test21.py
Traceback (most recent call last):
File "test21.py", line 4, in <module>
tree = ElementTree.parse(x)
File "C:\pg\Python37\lib\xml\etree\ElementTree.py", line 1197, in parse
tree.parse(source, parser)
File "C:\pg\Python37\lib\xml\etree\ElementTree.py", line 598, in parse
self._root = parser._parse_whole(source)
ValueError: multi-byte encodings are not supported
該当のソースコード
■Python # -*- coding: cp932 -*- from xml.etree import ElementTree x = 'fp4.xml' tree = ElementTree.parse(x) root = tree.getroot() print (root.tag) ■XMLファイル <?xml version="1.0" encoding="Shift_JIS"?> <root> <abc>1</abc> </root>
試したこと
XMLファイルの1行目を記述をなくすと正常終了します。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。