python 读取csv文件,无法读取第一列的数据,不知道为什么。以后有时间再来研究

import os
import csv
import json

fw = open("data_json.py", "w")
index = 0
with open("log_test/tests/data.csv", "r", newline='', encoding= u'utf-8',errors='ignore') as f:
reader = csv.DictReader(f)
for row in reader:
row_data = {
"name": row["name"],
"gender": row["gender"],
"score": row["score"]
}
fw.write("############# {} ##############\n".format(index))
fw.write("{}\n".format(json.dumps(row_data, indent=' ',ensure_ascii=False)))
index += 1


如果碰到‘UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position’问题,这是因为读取文件,并解析内容,但是有些文件的格式不是utf-8,导致读取失败,无法继续。

可以在open()函数中加上 encoding= u'utf-8',errors='ignore'两个参数试试。