开发者

csv module python3.1

开发者 https://www.devze.com 2023-01-28 04:44 出处:网络
Im using the following in Python2.x ; import csv f = open(\'test.csv\', \'wb\') writer = csv.writer(f) writer.writerow((fpath, md5sum, size)) # <str>, <str>, <int>

Im using the following in Python2.x ;

import csv
f = open('test.csv', 'wb')
writer = csv.writer(f)
writer.writerow((fpath, md5sum, size)) # <str>, <str>, <int>

This works without any problems. However, when I run this in Python3, I get a TypeError.

writer.writerow((fpath, md5sum, size))
TypeError: write() argument 1 must be bytes or buffer, not str

Of course, writing out the data to a file in opened in non-binary mode would do the trick, but I like the way U开发者_开发百科nicode is handled in Py3 and wish to specifically encode data before writing to a file and decode it when reading from it.

How do I solve this problem?


f = open('test.csv', 'w', encoding='utf-8', newline='')
0

精彩评论

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