【问题标题】:How to read simple xml file with lxml with iso-8859-1 encoding using child.text function如何使用 child.text 函数读取带有 iso-8859-1 编码的 lxml 的简单 xml 文件
【发布时间】:2019-06-13 13:51:56
【问题描述】:

我有以下简单结构的 xml 文件:

<?xml version="1.0" encoding="iso-8859-1"?>
<DICTIONARY>
    <Tag1>
        Übung1 Übersetzung1
        Übung2 Übersetzung2
        Übung3 Übersetzung3
        Übung4 Übersetzung4
        Übung5 Übersetzung5
    </Tag1>
    <Tag2>
        Übung6 Übersetzung6
        Übung7 Übersetzung7
        Übung8 Übersetzung8
        Übung9 Übersetzung9
        Übung10 Übersetzung10
    </Tag2>
</DICTIONARY>

我想用 lxml 读取这些文件,因为它很简单。我使用 child.text 读取文本部分,但编码似乎没有传递给输出字符串。请参阅下面的代码和输出。

我已经使用编解码器通过 iso-8859-1 读取文件,但它没有改变任何内容。

from lxml import etree
import codecs

def read_xml(): 
    taglist=[]
    new_dicts=[]
    with codecs.open("A:/test/test.txt", 'r', 
                     encoding='iso-8859-1') as xmlfile:
        try:
            tree=etree.parse(xmlfile)
            loaded=True
            print ("XML-encoding: ",tree.docinfo.encoding)
        except:
            loaded=False
            print ("""No dictionary loaded or xml structure is missing! Please try again!""")


    if loaded:

        root = tree.getroot()

        for child in root:
            new_dict={}
            tagname=child.tag
            taglist.append(tagname)

            print ("Loading dictionary for tag: ",
                   tagname)
            allstrings= child.text                
            allstrings=allstrings.split("\n")

            for line in allstrings:
                if line!=" " and line!="":
                    line=line.split("\t")
                    if line[0]!="" and line[1]!="":
                        enc_line0=line[0]
                        enc_line1=line[1]
                        new_dict.update({enc_line0:enc_line1})
            new_dicts.append(new_dict)

    return taglist, new_dicts
print (read_xml())

输出:

XML-encoding:  iso-8859-1
Loading dictionary for tag:  Tag1
Loading dictionary for tag:  Tag2
(['Tag1', 'Tag2'], [{'Ã\x9cbung1': 'Ã\x9cbersetzung1', 'Ã\x9cbung2': 'Ã\x9cbersetzung2', 'Ã\x9cbung3': 'Ã\x9cbersetzung3', 'Ã\x9cbung4': 'Ã\x9cbersetzung4', 'Ã\x9cbung5': 'Ã\x9cbersetzung5'}, {'Ã\x9cbung6': 'Ã\x9cbersetzung6', 'Ã\x9cbung7': 'Ã\x9cbersetzung7', 'Ã\x9cbung8': 'Ã\x9cbersetzung8', 'Ã\x9cbung9': 'Ã\x9cbersetzung9', 'Ã\x9cbung10': 'Ã\x9cbersetzung10'}])

然而,例如,我希望以与命令打印(“Übung”)相同的方式获得输出。我做错了什么?

【问题讨论】:

    标签: python-3.x lxml iso-8859-1


    【解决方案1】:

    lxml 适用于二进制文件。尝试改变

    with codecs.open("A:/test/test.txt", 'r', 
                     encoding='iso-8859-1') as xmlfile:
    

    with codecs.open("A:/test/test.txt", 'rb', 
                     encoding='iso-8859-1') as xmlfile:
    

    【讨论】:

    • 好的,即使读取为二进制文件,使用编码语句打开文件也无法再次工作,而没有编码语句的打开工作正常。谢谢!
    【解决方案2】:

    好的,我没有找到合适的解决方案,但是通过将所有内容转换为 UTF-8,我在进一步的步骤中没有问题 - 例如将单词与字典和其他字符串中的变音符号进行比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多