-1

I have a xml which i want to convert into csv but i am getting error.

In my xml file i wish to write only selected columns into csv.

import xml.etree.ElementTree as ET
import pandas as pd

root = ET.parse('D:\\Task\\09_ActionRecorder_0.XML').getroot()

tags =[]
for elem in root:
    for child in elem:
        try:
            tag = {}
            tag["TL"] = child.attrib['TL']
            tag["CN"] = child.attrib['CN']
            tag["DT"] = child.attrib['DT']
            tag["AN"] = child.attrib['AN']
            tags.append(tag)

        except KeyError:
            tags.append(tag)
print(tags)
df_users = pd.DataFrame(tags)
#df_users.head(20)


column_name_update = df_users.rename(columns = {"TL": "Title", 
                                  "CN":"Control Name", 
                                  "DT": "Date Time",
                                  "AN": "Application Name"}) 


#new_data.head(20)

column_name_update.to_csv("D:\\Tasks\\Sample.csv",index=False, columns=["Title", 'Control Name', 'Date Time', 'Application Name']) 

From the given xml file i wish to write only limited no of columns(as shown in code).But whenever i execute above code i am getting key error and in csv file only one column is being getting written.Kindly help if any one know how to do so.

2 Answers 2

1

Loop over list of xml files and convert each of them to csv

import xml.etree.ElementTree as ET

ATTRIBUTES = ['TL', 'CN', 'DT', 'AN']
data = []
# TODO populate the list - https://docs.python.org/2/library/os.html#os.listdir
list_of_files = []
for file_name in list_of_files:
    root = ET.parse(file_name)
    recs = root.findall('.//Rec')
    for rec in recs:
        data.append([rec.attrib.get(attr, 'N/A') for attr in ATTRIBUTES])
    with open('{}.csv'.format(file_name), 'w') as f:
        f.write('Title,Control Name,Date Time,Application Name' + '\n')
        for entry in data:
            f.write(','.join(entry) + '\n')
   data = [] 
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks but this code is for single xml file conversion. what I have to do if I have lets say 5 xml file which needed to be converted into individual csv file but i don't want to run code for those individually(agin and again).Is there any way where i just specify the folder which contain multiple xml file and write csv for those files individually.If you know anything about the same kindly let me know i will be highly thankful.
@Ani Do you need 1 csv per 1 xml or 1 csv for ALL xml files?
Well currently I am trying 1 csv per 1 xml but If Possible both the scenarios so that in future I might not face any problem because of the same.
@Ani I have modified the code to create 1 csv per xml file. You need to populate the list_of_files
And what about 1 csv for all xml if you know the same please add that also and thanks.
|
-1

I ran into a similar problem a few months ago, what I ended up doing was just using just using excel to save the file as a CSV, however in your case I know this might not be practical. What I would recommend is using your python file to first convert it to CSV using bash script(would also work with power shell) Then iterate over your CSV file.

This is how to create the bash script

This is how you can run the script from your python file

Hope this helps

1 Comment

Thanks but i dont have any knowledge regarding bash I want an python program where I can specify which columns I wanted to write from xml to csv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.