[Tutor] write dictionary to file

Ian D duxbuz at hotmail.com
Fri Jun 20 15:55:27 CEST 2014


Thanks


>
> Nonetheless, having re-read your question and having googled a bit, it
> seems that your problem might be related to Python 2 vs. Python 3, see
> here:
> http://stackoverflow.com/questions/24294457/python-typeerror-str-does-not-support-the-buffer-interface
>
> In short: In Python 2 you are expected to open the CSV file in binary
> mode ('wb'). In Python 3 this should be text mode as per the above
> question, else you'll only be able to write "bytes" streams, hence the
> "buffer" interface errors. If you've perhaps been cribbing/using
> Python 2.x examples and documentation while in fact using using Python
> 3, then that would help explain the confusion...?


Ok I see this error and the example shows a different type of syntax.


Rather than a file open for writing:

outfile = open('output.csv', 'wb')


it uses

with open('data.csv', 'w', newline='') as out:



now is this written differently in order to implement this text mode thing or is it just the omission of the 'b' on the 'wb' that causes text mode?



and if so could it be written:

outfile = open('output.csv', 'w')



? 		 	   		  


More information about the Tutor mailing list