我在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)