我使用以下代码解析了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输出?你知道吗
您可以创建一个文件名列表,然后在其中编写xml文件。如果输出文件已经在目录中,那么使用glob可以获得名称。如果文件不存在,下面的代码将使用给定的文件名创建
要从目录中获取XML文件名,可以尝试以下代码:
您的代码当前正在使用文件句柄来形成输出文件名。使用
file
代替f
,如下所示:相关问题 更多 >
编程相关推荐