Python etree.ElementTree IOError:[Errno 22]无效模式('rb')或文件名 -

时间:2015-08-19 01:18:55

标签: python zipfile xml.etree

我正在使用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

1 个答案:

答案 0 :(得分:1)

在这一行:

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

ET.parse需要文件或文件对象的名称;您正在传递文件的内容

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

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")