简体   繁体   English

如果编码不同于UTF-8,如何在python中读取xml文件

[英]how to read xml files in python if the encoding is different than UTF-8

I am trying to read an xml file with different encoding. 我正在尝试读取具有不同编码的xml文件。 In case of UTF-8 it is working fine but in case of other formats like GB18030 or BIG5 , it is throwing error like multi-byte encoding are not supported. 在使用UTF-8情况下,它可以正常工作,但在使用其他格式(例如GB18030BIG5情况下,则会抛出错误,例如不支持多字节编码。

Please suggest a solution for this. 请为此提出解决方案。 Thanks in advance. 提前致谢。

Here a try is open as file object and with ElementTree.fromstring() : 在这里尝试打开作为文件对象并使用ElementTree.fromstring()

import xml.etree.ElementTree as ET

with open('file_name.xml','r') as f:
   ef = ET.fromstring(f.read())

It was worked for me. 它为我工作。

Or you can do is with XMLParser : 或者您可以使用XMLParser

xmlp = ET.XMLParser(encoding="utf-8")
f = ET.parse('file_name.xml',parser=xmlp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM