首页 > python3 csv.write()问题

python3 csv.write()问题

import csv
csvfile = open(file, 'wb')
writer = csv.writer(csvfile)
writer.writerow(['title', 'summary', 'year', 'id', 'count', 'link'])
csvfile.close()

TypeError: a bytes-like object is required, not 'str'


我觉得问题出在python3使用open()打开文件,采用二进制读写。

那么应该怎么解决这个问题?


import csv

with open('test.csv', 'w') as csvout:
    writer = csv.writer(csvout)
    writer.writerow(['title', 'summary', 'year', 'id', 'count', 'link'])

我回答過的問題: Python-QA


试试这个:

import csv
csvfile = open(file, 'wb')
writer = csv.writer(csvfile)
writer.writerow([b'title', b'summary', b'year', b'id', b'count', b'link'])

Pythone3里面字符串和二进制数据是两种类型。

【热门文章】
【热门文章】