import csv,json
import pandas as pd
"""
奴隶社会,非洲,
奴隶社会,亚洲,
奴隶社会,亚洲,古印度
奴隶社会,欧洲,
"""
def csv2json(file_path):
with open(file_path,"r",encoding='UTF-8') as f:
reader = csv.reader(f,dialect='excel')
obj = {}
t1=t2=t3=""
for row in reader:
d1,d2,d3 = row[0],row[1],row[2]
if d1!="":
t1 = d1
obj[t1] = []
if d2!="":
t2 = d2
obj[t1].append({t2:[]})
if d3!="":
t3 = d3
obj[t1][-1][t2].append({t3:[]})
return obj
print(csv2json("./history.csv"))
数据处理-csv文件转化为json
最新推荐文章于 2025-06-07 11:55:35 发布
