使用Python将XML转换为Excel/CSV

2024-10-06 12:29:33 发布

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

我有一个包含xml数据的文件,但该文件使用一些自定义扩展名。还有一些空格(空单元格)。 这是文件:

<?xml version="1.0" encoding="utf-8"?>
<XMLData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PinList>
    <Pin>
      <Comment />
      <LedAdr>-1</LedAdr>
      <PinName />
      <System>1</System>
      <Type>DataType2</Type>
    </Pin>
    <Pin>
      <Comment />
      <LedAdr>-1</LedAdr>
      <PinName>Data1</PinName>
      <System>2</System>
      <Type>DataType1</Type>
    </Pin>
    <Pin>
      <Comment />
      <LedAdr>-1</LedAdr>
      <PinName>Data2</PinName>
      <System>3</System>
      <Type>DataType3</Type>
    </Pin>
    <Pin>
      <Comment />
      <LedAdr>-1</LedAdr>
      <PinName>Data3</PinName>
      <System>4</System>
      <Type>DataType2</Type>
    </Pin>
  </PinList>
  <CountFormat>c1a1</CountFormat>
  <Created>2019-09-11T13:46:49.7055676+02:00</Created>
  <Created_by>asd</Created_by>
  <Info>PinSystemNameTypeDecimal</Info>
  <AdapterPintableEnabled>false</AdapterPintableEnabled>
  <LastChanged>2020-05-04T20:13:23.4025733+02:00</LastChanged>
  <LastChanged_by>asd</LastChanged_by>
  <PinRange>5120</PinRange>
  <Version />
</XMLData>

我希望使用Python实现以下目标:

    A   |   B   |      C      |      D      |   E   |
   No    System  PinName       Type          Comment
   1     1.1     Data1         DataType1
   2     1.2     Data2         DataType2
   3     1.3     Data3         DataType3

最后一次触摸时,系统从1.1开始,但当它达到1.64时,接下来是2.1,而不是1.65

到目前为止,我的代码是:

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("SP.pintable")
root = tree.getroot()

f = open('sp.csv', 'w')

csvwriter = csv.writer(f)

count = 0
head = ["No", 'System','PinName','Type','Comment']

csvwriter.writerow(head)
for PinList in root.findall('PinList'):
    row = []
    No = 1
    row.append(No)
    System = PinList.find('Pin').find('System').text
    row.append(System)
    PinName = PinList.find('Pin').find('PinName').text
    row.append(PinName)
    Type = PinList.find('Pin').find('Type').text
    row.append(Type)
    Comment = PinList.find('Pin').find('Comment').text
    row.append(Comment)

    print(row)

    csvwriter.writerow(row)
f.close()

我有一个问题,它只适用于xml文件的第一行。我还增加了No=1的值,只是为了在csv中写入它,我不知道如何实现这个计数器,即“System”的值从1-10000,但在2.1之后写入1.1到1.64,而不是1.65

非常感谢


Tags: 文件nobytypepincommentxmlfind
1条回答
网友
1楼 · 发布于 2024-10-06 12:29:33

要使上述代码正常工作,请获取父标签PinList&;在它下面找到孩子的别针

import csv

head = ["No", 'System','PinName','Type','Comment']
fields = ["Comment", "LedAdr", "PinName", "System", "Type"]

with open("test.csv", "w") as f:
    csv_writer = csv.writer(f)

    csv_writer.writerow(head)
    for pin in root.find('PinList').findall('Pin'):
        row = []
        for field in fields:
            row.append(pin.find(field).text)

        csv_writer.writerow(row)

        # above loop can be re-written using list comprehension.
        # csv_writer.writerow([pin.find(field).text for field in fields])

注意:请遵循python命名约定。
What is the naming convention in Python for variable and function names?

相关问题 更多 >