如何将多个XML文件解析成多个CSV文件?

2024-09-30 22:15:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下代码解析了XML文件,该代码适用于单个XML输入到单个csv输出。我尝试使用glob来处理几个输入和几个csv输出,但我知道这是不正确的。你知道吗

import glob
import xml.etree.ElementTree as et
import csv

for file in glob.glob('./*.xml'):
    with open(file) as f:
        tree = et.parse(f)
        nodes = tree.getroot()

        with open(f'{f[:-4]}edited.csv', 'w') as ff:
            cols = ['dateTime','x','y','z','motion','isMoving','stepCount','groupAreaId','commit']
            nodewriter = csv.writer(ff)
            nodewriter.writerow(cols)
            for node in nodes:
                values = [ node.attrib.get(kk, '') for kk in cols]
                nodewriter.writerow(values)

如何更改以获得多个csv输出?你知道吗


Tags: csv代码inimportforaswithxml
2条回答

您可以创建一个文件名列表,然后在其中编写xml文件。如果输出文件已经在目录中,那么使用glob可以获得名称。如果文件不存在,下面的代码将使用给定的文件名创建

csvFileNames = ['outputfile1.csv', 'outputfile2.csv']
for file in csvFileNames:
    with open(file, 'w') as f:
        wtr = csv.writer(f)
        wtr.writerows( [[1, 2], [2, 3], [4, 5]]) # write what you want

要从目录中获取XML文件名,可以尝试以下代码:

from os import listdir
filenames = listdir('.') # here dot is used because script and csv files are in the same directory, if XML files are in other directory then set the path inside listdir
xmlFileNames = [ filename for filename in filenames if filename.endswith( ".xml" ) ]

# get xml file names like this, xmlFileNames = ["abc.xml", "ef.xml"]
resultCsvFileNameList = [fname.replace(".xml", ".csv") for fname in xmlFileNames ]

您的代码当前正在使用文件句柄来形成输出文件名。使用file代替f,如下所示:

import glob
import xml.etree.ElementTree as et
import csv

for file in glob.glob('./*.xml'):
    with open(file) as f:
        tree = et.parse(f)
        nodes = tree.getroot()

        with open(f'{file[:-4]}edited.csv', 'w') as ff:
            cols = ['dateTime','x','y','z','motion','isMoving','stepCount','groupAreaId','commit']
            nodewriter = csv.writer(ff)
            nodewriter.writerow(cols)
            for node in nodes:
                values = [ node.attrib.get(kk, '') for kk in cols]
                nodewriter.writerow(values)

相关问题 更多 >