我有一个包含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
非常感谢
要使上述代码正常工作,请获取父标签PinList&;在它下面找到孩子的别针
注意:请遵循python命名约定。
What is the naming convention in Python for variable and function names?
相关问题 更多 >
编程相关推荐