【发布时间】:2019-11-15 16:22:47
【问题描述】:
我使用此代码解析 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 输出?
【问题讨论】:
-
您的意思是使用
with open(f'{file[:-4]}edited.csv', 'w') as ff:吗?当前,您正在使用文件句柄作为文件名。 -
得到你的答案!
标签: python xml pandas csv elementtree