I have a file that contains xml data, but file use some custom extension. Also there are some blank spaces (empty cells). This is the file:
<?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>
I want to achieve the following using 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
With a final touch that System starts with 1.1 but when it reach 1.64 next to be 2.1 instead of 1.65.
My Code so far:
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()
I have a problem that it works only for 1st row of xml file. Also I have added value for No = 1 just to write it in csv, I have no idea how to achieve this counter that value of "System" is going from 1-10000 but to be written as 1.1 till 1.64 after that 2.1 instead of 1.65
Thanks a lot.