开发者

Python 3.1.3 Win 7: csv writerow Error "must be bytes or buffer, not str"

开发者 https://www.devze.com 2023-02-05 07:46 出处:网络
Got a simple script which worked perfectly under Python 2.7.1开发者_Python百科 at my Win xp machine.

Got a simple script which worked perfectly under Python 2.7.1开发者_Python百科 at my Win xp machine. Now got a win 7 machine with python 3.1.3.

The code is:

owriter.writerow(dtime[1][1])

dtime[1][1]=['30-Aug-10 16:00:00', '2.5', '15']

Got this error message: TypeError: must be bytes or buffer, not str

What changes should I make?

thanks.


In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling.

In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is:

outputfile=open("out.csv",'w',encoding='utf8',newline='')

encoding can be whatever you require, but newline='' suppresses text mode newline handling. On Windows, failing to do this will write \r\r\n file line endings instead of the correct \r\n. This is mentioned in the 3.X csv.reader documentation only, but csv.writer requires it as well.


Probably you need to open the file in text mode. If not, include enough of your code so it's runnable and demonstrates the problem.


Change to str.encode("ascii").

The point is that Python 2.x had somewhat mixed usage of str type for storing byte buffers and for storing character strings. Now in Python 3.x we have proper Unicode support and byte buffers are now separate type. You can convert between them using str.encode() and bytes.decode() specifying each time a character encoding as parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号