1 项目场景
Python 读取.csv文件问题:
import csv
with open('fileName_1.csv', 'rb') as csvfile:
reader = csv.reader(csvfile)
column = [row[0] for row in reader]
print(column)
2 问题描述
Error: iterator should return strings, not bytes (did you open the file in text mode?)
3 原因分析
因为.csv文件并非二进制文件, 只是一个文本文件。
4 解决方案
import csv
with open('fileName_1.csv', 'rt', encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
column = [row[0] for row in reader]
print(column)
或者
import csv
# open() 默认打开文本文件
with open('fileName_1.csv', 'r', encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
column = [row[0] for row in reader]
print(column)