繁体   English   中英

在文件夹中按文件打开文件

[英]Opening file by file in a folder

我是使用python编程的新手,但是目前我收到一个编写脚本的任务,该脚本将在type = 0或type = 1出现的所有ID都写下来。 它是一个XML文件,类似于以下示例:

<root>
<bla1 type="0" id = "1001" pvalue:="djdjd"/>
<bla2 type="0" id = "1002" pvalue:="djdjd" />
<bla3 type="0" id = "1003" pvalue:="djdjd"/>
<bla4 type="0" id = "1004" pvalue:="djdjd"/>
<bla5 type="0" id = "1005" pvalue:="djdjd"/>
<bla6 type="1" id = "1006" pvalue:="djdjd"/>
<bla7 type="0" id = "1007" pvalue:="djdjd"/>
<bla8 type="0" id = "1008" pvalue:="djdjd"/>
<bla9 type="1" id = "1009" pvalue:="djdjd"/>
<bla10 type="0" id = "1010" pvalue:="djdjd"/>
<bla11 type="0" id = "1011" pvalue:="djdjd"/>
<bla12 type="0" id = "1009" pvalue:="djdjd"/>

<root>

因此,代码要做的第一件事是将':='基本上替换为'='原因,这导致我的xml上传导致错误。 无论如何,它会写下类型为0的ID和类型为1的ID。这对于一个xml文件非常有效。 不幸的是,我只有一个文件,而且我需要一个像这样的循环:始终打开文件夹中的下一个xml文件(不同名称),并总是将新的ID添加到最后一个xml中找到的ID。 因此,基本上,它总是总是从新的xml文件中添加新找到的ID。

import xml.etree.cElementTree as ET # required import

    XmlFile = 'ID3.xml'  # insert here the name of the XML-file, which needs to be inside the same folder as the .py file

    my_file = open('%s' % XmlFile, "r+")  # open the XML-file
    Xml2String = my_file.readlines()  # convert the file into a list strings

    XmlFile_new = []  # new list, which is filled with the modified strings
    L = len(Xml2String)  # length of the string-list
    for i in range(1, L):  # Increment starts at 0, therefore, the first line is ignored
        if ':=' in Xml2String[i]:
            XmlFile_new.append(Xml2String[i].replace(':=', '='))    # get rid of colon
        else:
            XmlFile_new.append(Xml2String[i])

    tree = ET.ElementTree(XmlFile_new)
    root = tree.getroot()

    id_0 = []   # list for id="0"
    id_1 = []   # list for id="1"
    id_one2zero = []    # list for ids, that occur twice

    for i in range(len(root)):
        if 'type="0"' in root[i]:   # check for type
            a = root[i].index("id") + 5  # search index of id
            b = a+6
            id_0.append((root[i][a:b]))  # the id is set via index slicing
        elif 'type="1"' in root[i]:  # check for type
            a = root[i].index("id") + 5
            b = a+6
            id_1.append((root[i][a:b]))
        else:
            print("Unknown type occurred")  # If there's a line without type="0" or type="1", this message gets printed
            #  (Remember: first line of the xml-file is ignored)

    for i in range(len(id_0)):  # check for ids, that occur twice
        for j in range(len(id_1)):
            if id_0[i] == id_1[j]:
                id_one2zero.append(id_0[i])
    print(id_0)
    print(id_1)
    f = open('write.xml','w')
    print >>f, 'whatever'
    print('<end>')

解决此问题的一种简单方法是使用os.walk()函数。 有了它,您可以在一个目录中甚至递归地打开所有文件。

这是一个示例如何使用它:

for root, dirs, files in os.walk("your/path"):
    for file in files:
        # process your file

如果目录中除xml-files之外还有其他文件,则可以确保仅使用file.endswith(".xml")处理xml-files。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM