我正在尝试将csv转换为json,但无法获得预期的结果
Pyton代码:
import csv
import json
def csv_to_json(csvFile, jsonFile):
jsondict = {}
with open(csvFile, encoding='utf_8_sig') as csvfile:
csv_data = csv.DictReader(csvfile)
jsondict["data"]=[]
for rows in csv_data:
print(rows)
jsondict["data"].append(rows)
with open(jsonFile, 'w') as jsonfile:
jsonfile.write(json.dumps(jsondict, indent = 4))
csvfile = 'sample.csv'
jsonfile = 'sampleoutput.json'
csv_to_json(csvfile, jsonfile)
CSV数据示例:
<表类>
键值
tbody><<tr>100 value100-1 200 value200-1 200 value200-2 300 value300-1 300 value300-2 400 value400-1 400 value400-2 400 value400-3 表类>
上述预期结果需要在csv文件中分组数据使用下面的代码代替
import json
import io
import pandas as pd
raw_string = open("sample.csv", "r")
raw_string = ''.join([i for i in raw_string])
raw_data = [line.split(',') for line in raw_string.split()]
df = pd.read_csv(io.StringIO(raw_string))
for item in list(df.groupby(by=[i for i in df.columns if not i.endswith("Value")])):
df_temp = item[1]
# messy line that matches columns to their values. The list(set(x) makes it so values are unique but also json serializable
a = {k : (list(set(v)) if len(set(v)) > 1 else list(set(v))[0]) for k, v in df_temp.to_dict("list").items()}
print(json.dumps(a, indent=4))