[Tutor] write dictionary to file

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Jun 20 17:54:21 CEST 2014


On 20/06/2014 15:11, Ian D wrote:
>
>>
>> Ok making some progress by changing the 'wb' to 'w'
>>
>
> err no.
>
> unstuck again.
>
> import csv
>
> csvfile= open('StudentListToSort.csv', newline='')
> spamreader = csv.reader(csvfile,delimiter=',',quotechar='|')
> outfile = open('outfile.csv','w')
>
> for row in spamreader:
>
>      if row[4] == '6':
>          print("".join([row[0],'@email.com']),row[1])
>          email = "".join([row[0],'@email.com'])
>          output = email,row[1]
>          outfile.write(output)
>
> outfile.close()
>
> when I start to concatenate the results, it ends up as a Tuple and the write to file stuff doesn't like Tuples
>
> TypeError: must be str, not tuple
>

Please give the full traceback, not just the last line, as it gives us a 
lot more information.  As it happens you're creating a tuple when you 
assign output, as it's the comma that makes a tuple, so unless I've 
missed something there's your problem.

Further advice is to slow down a bit, remember more haste, less speed. 
Walking away from the problem for a few minutes to clear your head often 
works miracles :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com




More information about the Tutor mailing list