Python - For循环问题

时间:2016-03-11 16:33:08

标签: python

我正在尝试创建一个循环来遍历~3,000 .xml文件来搜索某些行并打印成一个txt文件。

我能在一个xml上使用的原始代码是:

my_file = open(r'C:\temp\20160309_test_xml\out.txt', 'w')

with open(r'C:\temp\20160309_test_xml\test.xml', 'r') as file:
    for name, lines in enumerate(file, 1):
        if "OBJNAM" in lines:
            my_file.write(file.next())
with open(r'C:\temp\20160309_test_xml\test.xml', 'r') as file:
    for number,  line in enumerate(file, 1):
        if "srfres" in line:
            my_file.write(file.next())
file.close()

我试图为此创建一个循环,但输出txt打印为空白:

import glob
import os

path = r'C:\temp\test_xml'
xml_directory = os.path.join(path, '*.xml')
xml_list = glob.glob(xml_directory)
my_file = open(r'C:\temp\20160309_test_xml\out.txt', 'w')

for xml in xml_list:
    for name, lines in enumerate(xml, 1):
        if "OBJNAM" in lines:
            my_file.write(file.next())
for xml in xml_list:
    for number,  line in enumerate(xml, 1):
        if "srfres" in line:
            my_file.write(xml.next())

my_file.close()
print

4 个答案:

答案 0 :(得分:0)

my_files = os.listdir("/all_files/") #put your input directory here
my_writer = open(r'C:\temp\20160309_test_xml\out.txt', 'w')
for fn in my_files:
    if not fn.endswith(".xml"):
         continue
    with open(fn, 'r') as f: #open and read from every xml file
         for line in f:
             if "OBJNAM" in line or "srfres" in line:
                  my_writer.write(line)
my_writer.close()

答案 1 :(得分:0)

或许您可以尝试使用列表理解

[(name,lines) for name,lines in enumerate(xml_list)]

答案 2 :(得分:0)

您似乎没有在循环中打开文件,而是在迭代路径。

我对您的代码提出的建议很少:

您可以迭代文件行:

with open(path, 'r') as file:
    for line in file:
        print line

此外,在处理xml文件时,您可以使用xml lib。

答案 3 :(得分:0)

import glob
import os

xmlDir = r'C:\temp\test_xml'
xmlFiles = glob.glob(os.path.join(xmlDir, "*.xml"))
outfilepath = r'C:\temp\20160309_test_xml\out.txt'
keywords = ['OBJNAM', 'srfres']

copy = False
with open(outfilepath, 'w') as outfile:
    for fpath in xmlFiles:
        with open(fpath) as infile:
            for line in infile:
                if copy: outfile.write(line)

                if any (w in line for w in keywords):
                    copy = True
                    continue
                else:
                    copy = False
                    continue