我有一个简单的XML数据,如下所示
<LocationList>
<Location dateTime="2018-11-17T00:11:01+09:00" x="2711.208" y="566.3292" z="0" motion="Walk" isMoving="True" stepCount="1" groupAreaId="1" commit="True" />
<Location dateTime="2018-11-17T00:11:02+09:00" x="2640.506" y="518.7352" z="0" motion="Walk" isMoving="True" stepCount="1" groupAreaId="1" commit="True" />
<Location dateTime="2018-11-17T00:11:03+09:00" x="2640.506" y="518.7352" z="0" motion="Stop" isMoving="False" stepCount="0" groupAreaId="1" />
<Location dateTime="2018-11-17T00:52:31+09:00" x="2516.404" y="574.0547" z="0" motion="Walk" isMoving="True" stepCount="1" groupAreaId="1" />
我试着把XML解析成csv文件
import xml.etree.ElementTree as et
import csv
tree = et.parse('./1_2018-11-17.xml')
nodes = tree.getroot()
with open('testxml1.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[kk] for kk in cols]
nodewriter.writerow(values)
但是,由于并非所有的XML行都具有“stepCount”、“groupAreaId”、“commit”的值,因此除非删除这些变量,否则代码将无法工作。你知道吗
如何才能获得csv文件中显示的所有变量,包括变量上有空值的行?你知道吗
如果使用.get()方法读取节点属性,则可以添加默认值,如空字符串。所以你的情况是这样的:
您可以在列表中使用if else语句来检查属性是否存在。你知道吗
相关问题 更多 >
编程相关推荐