【发布时间】:2019-05-31 05:31:18
【问题描述】:
我在 spyder python 中使用大 csv 数据将 csv 转换为 json,但它显示错误字段大于字段限制 (131072)。
转换脚本:
import csv
import json
file = r'abcdata.csv'
json_file = r'abcdata.json'
#Read CSV File
def read_CSV(file, json_file):
csv_rows = []
with open(file) as csvfile:
reader = csv.DictReader(csvfile)
field = reader.fieldnames
for row in reader:
csv_rows.extend([{field[i]:row[field[i]] for i in range(len(field))}])
convert_write_json(csv_rows, json_file)
#Convert csv data into json
def convert_write_json(data, json_file):
with open(json_file, "w") as f:
f.write(json.dumps(data, sort_keys=False, indent=1, separators=(',', ': '))) #for pretty
f.write(json.dumps(data))
read_CSV(file, json_file)
【问题讨论】:
-
请添加堆栈跟踪。并添加更多关于您正在使用的数据、行/列数、文件大小等的数据。
标签: python json csv data-conversion