我尝试将我的xml文件转换为csv文件:
这是我的xml文件的一个示例:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="pdml2html.xsl"?>
<!-- You can find pdml2html.xsl in C:\Program Files\Wireshark or at https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=pdml2html.xsl. -->
<pdml version="0" creator="wireshark/2.2.8" time="Thu Oct 12 14:41:47 2017" capture_file="D:\Packet_Analyse_v4.pcapng">
<packet>
<proto name="geninfo" pos="0" showname="General information" size="100">
<field name="num" pos="0" show="1" showname="Number" value="1" size="100"/>
<field name="len" pos="0" show="100" showname="Frame Length" value="64" size="100"/>
<field name="caplen" pos="0" show="100" showname="Captured Length" value="64" size="100"/>
<field name="timestamp" pos="0" show="Oct 10, 2017 18:05:22.371598000 Central European Daylight Time" showname="Captured Time" value="1507651522.371598000" size="100"/>
</proto>
</packet>
</pdml>
我使用那些代码行:
import xml.etree.ElementTree as ET
root = ET.parse('test.xml').getroot()
with open("output.csv", "wb") as file:
for child in root:
print(child.tag +','+ child.attrib)
# naive example how you could save to csv line wise
file.write(child.tag+";"+child.attrib)
但我收到了这个错误:
file.write(child.tag+";"+child.attrib)
TypeError: Can't convert 'dict' object to str implicitly