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

2024-06-25 06:53:22 发布

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

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


Tags: 文件csvimporttruedatetimelocationxmlmotion
2条回答

如果使用.get()方法读取节点属性,则可以添加默认值,如空字符串。所以你的情况是这样的:

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

您可以在列表中使用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)

相关问题 更多 >