Possible Duplicate:
Creating a dictionary from a CSV file
I am having an issue when trying to print a dictionary to a CSV file. The dictionary currently has 4 columns but all 4 columns are printed in 1 single column. It may be the for loop that I am using to write to the CSV file is to blame but I am not quite sure. I am trying to have the dictionary print in each column.
Sample Data:
Date First Name Last Name Score
12/28/2012 15:15 John Smith 20
12/29/2012 15:15 Alex Jones 38
12/30/2012 15:15 Michael Carpenter 25
Below are some code excerpts:
import csv
csvWriter = csv.writer(open("C:\\Users\\Chris\\Desktop\\test_out.csv", 'w'), delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
For loop to read in CSV file above:
for row in reader:
for k, v in row.items():
if not k in mydict:
mydict[k] = [v]
else:
mydict[k].append(v)
For loop to print Dictionary keys to CSV file.
for item in mydict.keys():
csvWriter.writerow(item)
Current output (column headings):
D a t e
F i r s t N a m e
L a s t N a m e
S c o r e
Wanted output (column headings):
Date First Name Last Name Score
Any help would be greatly appreciated.