csv.Error: iterator should return strings, not bytes

xiaoxiao2021-02-28  100

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)
转载请注明原文地址: https://www.6miu.com/read-58193.html

最新回复(0)