如何将XML文件中的某些缺少值的XML转换为CSV?

时间:2019-07-04 15:07:03

标签: python xml pandas csv elementtree

我有一个简单的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文件中显示的所有变量,包括变量中带有空值的行?

2 个答案:

答案 0 :(得分:2)

如果使用.get()方法读取node属性,则可以添加默认值,例如空字符串。因此,您的情况将是这样的:

for node in nodes:
        values = [ node.attrib.get(kk, '') for kk in cols]
        nodewriter.writerow(values)

答案 1 :(得分:1)

您可以在列表推导中使用if-else语句来检查属性是否存在。

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:
        # if kk is not an attribute, set the value to None
        values = [node.attrib[kk] if kk in node.attrib else None for kk in cols]
        # Replace commit value with false if it does not exist
        if values[-1] is None:
            values[-1] = False
        nodewriter.writerow(values)
相关问题