[Tutor] write dictionary to file

Ian D duxbuz at hotmail.com
Mon Jun 23 11:17:44 CEST 2014


>>
>> import csv
>>
>> csvfile= open('StudentListToSort.csv', newline='')
>> spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|')
>
> Are you sure that your input file uses | as a quote character and , as
> the field delimiter?


No I overlooked this


>
>
>> #open a file to write to later
>> fields = ['user','first','last','password','year']
>> csvoutput = open('output.csv', 'wb+')
>
> I'm pretty sure you don't want to use "wb+" mode. Since you're using
> Python 3, I think you should just use "w" mode.
>
> The "b" turns on binary mode, and in Python 3 you don't want that. The
> "+" turns on either "read/write" mode or "append" mode, I don't remember
> which, but either way I don't think it's necessary for what you are
> doing.


Yes I came across this python 3 idiosyncrasy



>
>
>> spamwriter = csv.DictWriter(csvoutput,fieldnames=fields, delimiter=' ')
>
> Now you're turning every , delimiter into a space. Are you sure you want
> that?
>
>


Overlooked this



>> for row in spamreader:
>> if row['year'] == '40':
>> username = row['user']
>> email = "".join([username,'@email.com])
>
> Syntax error: you left out the closing single quote. You need:
>
> email = "".join([username,'@email.com'])
>


I think I may have messed that up editing code for public viewing.




On to your example.....



>
> import csv
>
> # Open the file we're reading from.
> csvfile= open('StudentListToSort.csv', newline='')
> # Open a file to write to.
> csvoutput = open('output.csv', 'w', newline='')
>
> fields = ['user', 'first', 'last', 'password', 'year']
>
> # Are you sure you want | as the quote character?
> spamreader = csv.DictReader(csvfile, delimiter=',', quotechar='|')

>
>
> # Still using , as a delimiter, not space.
> spamwriter = csv.DictWriter(csvoutput, fieldnames=fields, delimiter=',')
>
> for row in spamreader:
> if row['year'] == '40':
> email = row['user'] + '@email.com'
> output = [ row[fieldname] for fieldname in fields ]


I am unsure about this syntax [ row[fieldname] for fieldname in fields ]

 

The FOR loop is not in any context I have used before. I have seen examples(recently) so its obviously standard practice, but not something I would ever think to type. 



> print(output)
> # DictWriter needs a dict, not a list.
> spamwriter.writerow({name: row[name] for name in fields})
> print("Warning: email calculated but never used:", email)


 

And this writerow syntax is something new for me, as are dictionaries( which I have tried to read up and understand.)

 

>spamwriter.writerow({name: row[name] for name in fields})

 

This looks like the same loop as the one above but a dictionary using curly braces(for dict), its the same unfamiliar way of writing a FOR loop for me.


 

So if I wanted multiple rows in the output csv file I would try:


({name: row[name], row[email, row[first] for name in fields})


which doesn't work as the syntax is invalid, so what would I need to change to allow spamwriter.writerow to generate multiple fields? 		 	   		  


More information about the Tutor mailing list