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='')
精彩评论