[Tutor] write dictionary to file

Ian D duxbuz at hotmail.com
Thu Jun 19 17:13:45 CEST 2014


When trying to write my dictionary to a file I get:


f.write(output)
TypeError: 'tuple' does not support the buffer interface


using this example:




#so far this should read a file
#using dictreader and take a column and join some text onto it


import csv


csvfile= open('StudentListToSort.csv', newline='')
spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|')


#open a file to write to later
f = open('output.csv', 'wb+')


#iterate through dictreader object
for row in spamreader:
    if row['year'] == '40':     
        username = row['user'] #I put stuff in variables for ease of viewing/debugging
        email = "".join([username,'@email.com]) # join text
        
        
        #put output for file in variable first
        output = email, row['first'],row['last'],row['password']
        
        #change tuple to list
        #outputlist= list(output)
        
        #write results to file   
        f.write(output)
        print(output)


I then tried to cast the tuple to a list thinking that would help like this:


        #change tuple to list
        #outputlist= list(output)
        
        #write results to file   
        f.write(outputlist)
        print(outputlist)


same problem:


f.write(outputlist)
TypeError: 'list' does not support the buffer interface


So is it the csv.DictWriter that is needed here? 		 	   		  


More information about the Tutor mailing list