读取包含 UTF-8 xml 文件的 zip 时出现问题 [英] Issues reading zip containing UTF-8 xml files

查看:37
本文介绍了读取包含 UTF-8 xml 文件的 zip 时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含许多 UTF-8 xml 文件的 zip 档案.这些文件主要有英文标签和文本,但也有一些标签包含非英文文本.我打开 zip 文件并解析其中的 xml 文件没有问题,但非英文文本丢失了它的编码.

在 Notepad++ 中提取并打开 xml 文件时,非英文文本如下所示:

Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирже (У17Бирже)

当它被提取出来并用 Python 读取时(在 Linux 机器上),文本看起来像:

<预> <代码>ÐÑÑÑкаND±Ð¾Ð²Ð°Ð½ÑакÐ'олd»d°NNнÐμDD·Ð¼ÐμнилNNнаd£ÐºÑаинÑкойÐÐμжбd°Ð½ÐºÐ¾Ð²ÑкойÐалÑÑнойÐиÑже (УÐÐÐ) - 176.100.

我的代码如下:

def 解析(self, fp):# 打开/解压zip文件使用 zipfile.ZipFile(fp, 'r') 作为 f:# 获取所有压缩文件comp_files = f.namelist()对于 comp_files 中的 comp_file:cfp = f.open(comp_file, 'r')#解析xml树 = ElementTree.parse(cfp)...解析...

我尝试对 cfp 中的文本进行解码/编码,并使用 codecs.EncodedFile() 和 utf_8 和 utf_8_sig 的输入编码对其进行包装,而没有任何更改.我该怎么做才能修复非英文文本?

解决方案

您看到的结果是 UTF-8 被错误地解码为 latin-1/iso-8859-1:

<预><代码>>>>x=u'Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирже (У17'Б.10.1)>>>打印 x.encode('utf8').decode('latin1')ÐÑÑÑкаND±Ð¾Ð²Ð°Ð½ÑакÐ'олd»d°NNнÐμDD·Ð¼ÐμнилNNнаd£ÐºÑаинÑкойÐÐμжбd°Ð½ÐºÐ¾Ð²ÑкойDD°d»ÑÑнойÐиÑже (УÐÐÐ) - 176.100.

我将通过 Notepad++ 编码的以下文本保存为一个单独的文件,编码为 UTF-8,没有 BOM 的 zip 文件:

<text>Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирбой Валютной Бился на Украинской Межбанковской Валютной Бирбой Валютной Бирук доллару не изменился на

经过修改以使其可运行的代码:

from xml.etree import ElementTree导入压缩文件定义解析(fp):# 打开/解压zip文件使用 zipfile.ZipFile(fp, 'r') 作为 f:# 获取所有压缩文件comp_files = f.namelist()对于 comp_files 中的 comp_file:cfp = f.open(comp_file, 'r')#解析xml树 = ElementTree.parse(cfp)打印 tree.getroot().text打印类型(tree.getroot().text)解析(打开('file.zip'))

结果:

Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирже (У17Бирже)<输入'unicode'>

所以在我看来,它只是在您的 Linux 机器上显示不正确,但如果没有您正在使用的文件的实际样本,就很难进一步分析.

I have zip archives containing many UTF-8 xml files. These files have mostly English tags and text but a few tags contain non-English text. I have no problem with opening the zip file, and parsing the xml files inside of it, but the non-English text looses it's encoding.

When an xml file is extracted and opened in Notepad++ the non-English text looks like:

Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирже (УМВБ) - 176.100.

When it is extracted and read in Python (on a linux box) the text looks like:

ÐÑÑÑ ÐºÐ°ÑбованÑа к доллаÑÑ Ð½Ðµ изменилÑÑ Ð½Ð° УкÑаинÑкой ÐежбанковÑкой ÐалÑÑной ÐиÑже (УÐÐÐ) - 176.100.

My code looks like:

def parse(self, fp):
    # open/decompress zip file
    with zipfile.ZipFile(fp, 'r') as f:
        # get all files in zip
        comp_files = f.namelist()
        for comp_file in comp_files:
            cfp = f.open(comp_file, 'r')
            # parse xml
            tree = ElementTree.parse(cfp)
            ...parsing...

I have tried decoding/encoding the text from cfp and wrapping it with codecs.EncodedFile() and input encoding of utf_8 and utf_8_sig with no change. What can I do to fix the non-English text?

解决方案

The result you are seeing is UTF-8 incorrectly decoded as latin-1/iso-8859-1:

>>> x=u'Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирже (УМВБ) - 176.100.'
>>> print x.encode('utf8').decode('latin1')
ÐÑÑÑ ÐºÐ°ÑбованÑа к доллаÑÑ Ð½Ðµ изменилÑÑ Ð½Ð° УкÑаинÑкой ÐежбанковÑкой ÐалÑÑной ÐиÑже (УÐÐÐ) - 176.100.

I saved the following text encoded via Notepad++ as as a single file encoded as UTF-8 without BOM in a zipfile:

<text>Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирже (УМВБ) - 176.100.</text>

Your code with modifications to make it runable:

from xml.etree import ElementTree
import zipfile

def parse(fp):
    # open/decompress zip file
    with zipfile.ZipFile(fp, 'r') as f:
        # get all files in zip
        comp_files = f.namelist()
        for comp_file in comp_files:
            cfp = f.open(comp_file, 'r')
            # parse xml
            tree = ElementTree.parse(cfp)
            print tree.getroot().text
            print type(tree.getroot().text)

parse(open('file.zip'))

The result:

Курс карбованца к доллару не изменился на Украинской Межбанковской Валютной Бирже (УМВБ) - 176.100.
<type 'unicode'>

So it looks to me that it is just being displayed incorrectly on your Linux box, but without an actual sample of the files you are working with, it is difficult to analyze further.

这篇关于读取包含 UTF-8 xml 文件的 zip 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆