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.