python3.x - python3 csv.write()问题

查看:307
本文介绍了python3.x - 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'])

  • 請使用 with 來進行文件的開關, 而且你原本的 code 只有開沒有關可能會有問題


我回答過的問題: Python-QA

这篇关于python3.x - python3 csv.write()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆