最近再看python处理csv的内容,查阅之前有python2 打开csv文件的操作:
writer = csv.writer (open('test.csv','wb')
但是在python3处理起来一直报错
TypeError: a bytes-like object is required, not 'str'
查阅文献后发现
n 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:
发现在python2中要用二进制打开文件,进行line termination handling,但是在python3中,这种方式是不能用了改成:
writer = csv.writer (open('test.csv','w',encoding='utf8',newline=''))即可