Sheetal Patel
Sheetal Patel

Reputation: 57

Trying to convert xml into csv

I'm trying to convert xml into csv using python and I have attached my code below. I don't know where I'm going wrong.. it's not happening.. I tried different ways and after several changes and modifications, I'm stuck with this.

I thought this was legit, but still its not working.
Could anyone help me out and tell me where I'm going wrong?

When I try to run it.. it prints (xxxxxxxxxxxx) can anyone help me out?

                needed_data = open('C:\\Users\\dv\\NeededData.csv', 'w')
                csvwriter = csv.writerow(need_data)
                count = 0
                head = ['username']
                csvwriter.writerow(head)

                for process_inf in root.findall('processing_info'):
                    row = []
                    u_name = process_inf.find('username').text
                    row.append(username)

                    csvwriter.writerow(needed_head)
                needed_data.close()

sample xml file :

<processing_info username="437896" time="1536506399" done_by="BIBO"/> 

desired csv:

username
437896
.
.
.
.
etc etc

Upvotes: 0

Views: 67

Answers (1)

larsks
larsks

Reputation: 312620

You are explicitly checking to see if the caller has passed an argument on the command line:

if len(sys.argv) is not 2:
    print("xxxxxxxxxxx.")

If your code is printing xxxxxxxxxxx. then you are not providing the expected argument. For example, if I place your code, unmodified, into a file called readerusers.py, if I run it like this:

python readusers.py

I get this output:

xxxxxxxxxxx.

But if I provide an argument:

python readuers.py something

Then it produces no output.


There are various other problems with your code; one that jumps out is this:

needed_data = open('C:\\Users\\dv\\NeededData.csv', 'w')
csvwriter = csv.writerow(need_data)

I think you mean:

csvwriter = csv.writer(need_data)

Upvotes: 1

Related Questions