【问题标题】:Python etree.ElementTree IOError: [Errno 22] invalid mode ('rb') or filename -Python etree.ElementTree IOError: [Errno 22] 无效模式 ('rb') 或文件名 -
【发布时间】:2015-08-19 01:18:54
【问题描述】:

我正在使用 zipfile 模块,每当我尝试读取提取的 XML 数据时,我都会收到此错误,我不太理解,我尝试使用不同的模式来解决它,但没有奏效。

Traceback (most recent call last):
  File "C:\python\QuarryLang-master\core\test.py", line 18, in <module>
    p = load_data("ConlangExample.zip")
  File "C:\python\QuarryLang-master\core\cio.py", line 71, in load_data
    meta = ET.parse(zipdata.open('metadata.xml', 'rU').read())
  File "C:\python\App\lib\xml\etree\ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
  File "C:\python\App\lib\xml\etree\ElementTree.py", line 647, in parse
    source = open(source, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: '<metadata><conlang>Pseudo-German</conlang><author>Andres</author><family>Germanic</family><T_Type>Agglulinative</T_Type

这是我的代码:

import zipfile
import xml.etree.ElementTree as ET
import os
import conassets
def load_configuration(filename):
    settings = ET.parse(filename)
    root = settings.getroot()
    return (root.find('appname').text, root.find('author').text, root.find('version').text)



def save_data(filename, conlang, zpack=0):
    xml_metadata = ET.Element("metadata")
    xml_dictionary = ET.Element("dictionary")
    xml_dialect = ET.Element("dialects")


    #Set up metadata first
    ET.SubElement(xml_metadata, "conlang").text = conlang.Name
    ET.SubElement(xml_metadata, "author").text = conlang.Author
    ET.SubElement(xml_metadata, "family").text = conlang.Family
    ET.SubElement(xml_metadata, "T_Type").text = conlang.T_Type
    ET.SubElement(xml_metadata, "A_Type").text = conlang.A_Type
    ET.SubElement(xml_metadata, "L_Type").text = conlang.L_Type

    mtree = ET.ElementTree(xml_metadata)
    mtree.write("metadata.xml")

    #Set up dictionary
    if len(conlang.words) != 0:
            for x in conlang.words:
                word = ET.SubElement(xml_dictionary,"word")
                ET.SubElement(word, "word").text = x.word
                ET.SubElement(word, "definition").text = x.definition 
                ET.SubElement(word, "pos").text = x.pos
                ET.SubElement(word, "register").text = x.register
                ET.SubElement(word, "class").text = x._class
                ET.SubElement(word, "dialect").text = x.dialect
                ET.SubElement(word, "source lang").text = x.source_lang
                ET.SubElement(word, "source").text = x.source
                ET.SubElement(word, "notes").text = x.notes


            dtree = ET.ElementTree(xml_dictionary)
            dtree.write("dictionary.xml")
    if len(conlang.dialects) != 0:
        #Write dialects
        for x in conlang.dialects:
            dialect = ET.SubElement(xml_dialect, "dialect")
            ET.SubElement(dialect, "name").text = x.Name
            ET.SubElement(dialect, "description").text = x.description

        ditree = ET.ElementTree(xml_dialect)
        ditree.write("dialects.xml")
    if zpack == 0 or zpack == 3:
        with zipfile.ZipFile(filename, "w") as myconlang:
            myconlang.write("metadata.xml")
            myconlang.write("dictionary.xml")
            myconlang.write("dialects.xml")
            myconlang.close()
        if zpack != 3:
            os.remove('metadata.xml')
            os.remove('dictionary.xml')
            os.remove('dialects.xml')



def load_data(filename):
    zipdata = zipfile.ZipFile(filename, 'r')
    meta = ET.parse(zipdata.open('metadata.xml', 'rU').read())
    dictionary = ET.parse(zipdata.open('dictionary.xml', 'rU').read())
    dialect = ET.parse(zipdata.open('dialect.xml', 'rU').read())

    meta_root = meta.getroot()
    dict_root = dictionary.getroot()
    dial_root = dialect.getroot()

    #Get metadata
    C = Conlang(meta_root.find('conlang').text, meta_root.find('author'), meta_root.find('family').text, meta_root.find('T_Type').text, meta_root.find('A_Type').text, meta_root.find('L_Type').text)

    #Get dictionary data
    for word in dict_root.findall('word'):
        W = Word(word.find('word').text, word.find('definition').text, word.find('ipa').text, word.find('register').text, word.find('class').text, word.find('dialect'), word.find('source_lang').text, word.find('source').text, word.find('notes').text)
        W.add2list(C)

    #Load all dialect data
    for dialect in dial_root.findall('dialect'):
        D = Dialect(dialect.find('name').text, dialect.find('description').text)
        D.add2list(C)


    return C

【问题讨论】:

    标签: python zipfile xml.etree


    【解决方案1】:

    在这一行:

    meta = ET.parse(zipdata.open('metadata.xml', 'rU').read())
    

    ET.parse 需要文件或文件对象的 name;您正在向它传递文件的内容。

    尝试传入类似文件的对象:

    meta = ET.parse(zipdata.open('metadata.xml', 'rU'))
    

    或者尝试改用ET.fromstring:

    meta = ET.fromstring(zipdata.open('metadata.xml', 'rU').read())
    

    这是一个完整的测试程序:

    import zipfile
    import xml.etree.ElementTree as ET
    import sys
    
    def load_data(filename):
        zipdata = zipfile.ZipFile(filename, 'r')
        meta = ET.parse(zipdata.open('metadata.xml', 'rU'))
        meta.write(sys.stdout)
    
    load_data("foo.zip")
    

    【讨论】:

    • 我在使用您的建议时收到此错误:Traceback(最近一次通话最后一次):ParseError: not well-formed (invalid token): line 1, column 175 它通常指向ElementTree.py
    • 我刚刚发布的测试程序在 Python 2.7.6、Ubuntu 14.04.3 上完美运行。它对你有用吗?
    • 您提到的错误“ParseError: not well-formed”意味着您的 XML 数据不是正确的 XML。解压 ZIP 文件,查看第 1 行第 175 列,看看是否能找出问题所在。
    猜你喜欢
    • 1970-01-01
    • 2014-04-25
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    相关资源
    最近更新 更多