2

I am a newbie to python and here is what I am trying to achieve. I am trying to export a python created list as a csv file but it does not seem to work.

What I am expecting to see:

Name    Address    ID
A         AA       01
B         BB       05
..       ....     ....

But this is what my output looks like unfortunately:

Name    Address    ID
['A', 'AA', '01']
['B', 'BB', '05']

In other words it is clubbing all the elements of a row in the list into the first column of the output CSV file while the header elements are in their respective columns. I am hoping to populate the entries under their respective headers in the output file. I have not encountered this problem if I am reading from a csv file and writing to one.

Here's what my code looks like:

import csv
fOpen1=open('C:\master.csv')
fOpen2=open('C:\test.csv')

masterFile=csv.reader(fOpen1)
testFile=csv.reader(fOpen2)   

outputFile=open('C:\Output.csv', 'wb')
CSVWriter=csv.writer(outputFile)

masterList=list()
testList=list()
header=testFile.next()
CSVWriter.writerow(header)

for row1 in masterFile:
    if row1[0] not in masterList:
        masterList.append(row1[0])

for row2 in testFile:
    if row2[0] not in masterList:
        testList.append(row2)

CSVWriter.writerows(zip(testList))

Where do you think I am going wrong with my code? Any help and advice would be greatly appreciated.

0

1 Answer 1

1

The issue is that you are doing -

CSVWriter.writerows(zip(testList))

This causes each element in the list passed to writerows() to be a tuple with a single element, which is the list (which you see outputed to the csv). Example -

>>> l = [[1,2,3,4],[4,5,6,7],[5,5,5,5]]
>>> list(zip(l))
[([1, 2, 3, 4],), ([4, 5, 6, 7],), ([5, 5, 5, 5],)]

If you directly want to write the rows in testList, you should do -

CSVWriter.writerows(testList)

If you want to transpose the testList, so that rows become columns and columns become rows, you should unpack when sending into zip() -

CSVWriter.writerows(zip(*testList))
Sign up to request clarification or add additional context in comments.

3 Comments

I was initially using the write command without zip and it wasn't working because it was transposing the rows into columns. So I started using zip. Now I removed the zip from the code and it is working perfectly fine. I feel quite stupid now. I wonder what wrong I was doing. Thank you for your quick response. It is much appreciated.
You will need to show the exact code that does not work if you want to know what went wrong, maybe you were transposing the rows/columns in some other place causing the issue.
I have absolutely no idea now as I was continually saving the script as I was making changes. But transposing is still not clear to me. I will worry about transposing at a later stage. Thanks.

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.