如何使用Python将CSV转换为JSON,其中一些JSON键有多个值



我正在尝试将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>100value100-1200value200-1200value200-2300value300-1300value300-2400value400-1400value400-2400value400-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))

相关内容

  • 没有找到相关文章