I am trying to take data from API call that returns XML object and parse few data points into a csv file with each object in its own column.
The XML looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<YourMembership_Response>
<Items>
<Item>
<ItemID></ItemID>
<ID>92304823A-2932</ID>
<WebsiteID>0987</WebsiteID>
<NamePrefix></NamePrefix>
<FirstName>John</FirstName>
<MiddleName></MiddleName>
<LastName>Smith</LastName>
<Suffix></Suffix>
<Nickname></Nickname>
<EmployerName>abc company</EmployerName>
<WorkTitle>manager</WorkTitle>
<Date>3/14/2013 2:12:39 PM</Date>
<Description>Removed from group by Administration.</Description>
</Item>
<Item>
<ItemID></ItemID>
<ID>92304823A-2932</ID>
<WebsiteID>0987</WebsiteID>
<NamePrefix></NamePrefix>
<FirstName>John</FirstName>
<MiddleName></MiddleName>
<LastName>Smith</LastName>
<Suffix></Suffix>
<Nickname></Nickname>
<EmployerName>abc company</EmployerName>
<WorkTitle>manager</WorkTitle>
<Date>3/14/2013 2:12:39 PM</Date>
<Description>Removed from group by Administration.</Description>
</Item>
I have written this code to write just IDs into CSV, which works fine.
with open("output1.csv", "wb") as f:
writer = csv.writer(f)
for node in tree.findall('.//ID'):
writer.writerow([node.text])
Now when I attempting to write multiple data points into csv, the machine is simply appending the data points into one column. This the code here I have been attempting with:
with open("test1.csv", "wb") as f:
writer = csv.writer(f)
for node in tree.findall('.//ID'):
writer.writerow([node.text])
for node in tree.findall('.//FirstName'):
writer.writerow([node.text])
for node in tree.findall('.//LastName'):
writer.writerow([node.text])
I need the data to look like this in the csv with other data points of choosing later on, what am I doing wrong?:
ID FirstName LastName
92304823A-2932 John Smith
Thank you in advance.