zipfile 读取压缩包里的xml内容为gbk时报错

我在使用zipfile读取压缩包里的xml内容时因为内容时gbk的,报错:

ValueError: multi-byte encodings are not supported

以下为xml的内容,xml最后一行会多出一个空行:

<?xml version="1.0" encoding="GBK"?>
<data-bus>
  <TONGZHISXJ>
    <FUJIANLB><!-- 空记录 --></FUJIANLB>
  </TONGZHISXJ>
</data-bus>

以下是我读取zip里xml内容的代码:

"""zip内容有:
1.jpg
2.png
3.xml等
"""
zipfilename="E:\python\1.zip"
with zipfile.ZipFile(zipfilename, 'r') as zfile:
        nl = zfile.namelist()
        for id,xl in enumerate(nl):
            if ".xml" in xl:
                aa=zfile.open(xl,'r')
                xml_srt=aa.read()
                doc=parseString(xml_srt)#这里出现ValueError: multi-byte encodings are not supported

python3.7,win10
百度和谷歌都没有找到处理方法,有大佬知道怎么解决么?

Jason990420
最佳答案

Not sure if it work in your case, or not.

xml_srt=aa.read().decode(encoding="GBK")
1年前 评论
shenmadouyao (楼主) 1年前
shenmadouyao (楼主) 1年前
讨论数量: 3
Jason990420

Not sure if it work in your case, or not.

xml_srt=aa.read().decode(encoding="GBK")
1年前 评论
shenmadouyao (楼主) 1年前
shenmadouyao (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!