csv.Error: iterator should return strings, not bytes

来源:互联网 发布:怎样成为淘宝客挣钱 编辑:程序博客网 时间:2024/06/14 01:59

python 读取csv文件问题

with open("fer2013.csv", "rb", encoding="utf-8") as vsvfile:   reader = csv.reader(vsvfile)   rows = [row for row in reader]   print(rows)

输出:

Error: iterator should return strings, not bytes (did you open the file in text mode?)

问题分析

因为此csv文件并非二进制文件, 只是一个文本文件。

问题解决

with open("fer2013.csv", "rt", encoding="utf-8") as vsvfile:   reader = csv.reader(vsvfile)   rows = [row for row in reader]   print(rows)

或者

# 因为open()默认打开文本文件with open("fer2013.csv", "r", encoding="utf-8") as vsvfile:   reader = csv.reader(vsvfile)   rows = [row for row in reader]   print(rows)
阅读全文
1 0