多个CSV到JSON文件转换

yiytaume  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(89)

我想在Python中将CSV转换为JSON。
我的输入文件是这样的:

的数据
输出文件应该是这样的:

{"translated_string":"¿Podrías buscar requisiciones de Editor Digital con habilidades de estrategia de clientes?",
  "skills":[{"skill":"estrategia de clientes",
     "startingPosition":67,
     "endingPosition":88}],
  "titles":[
   {"title":"Editor Digital",
     "startingPosition":33,
     "endingPosition":46}]}

字符串

eanckbw9

eanckbw91#

可能有不那么复杂的方法来做到这一点,但这应该适用于问题中概述的特定情况。

import pandas as pd
import re
from json import loads, dumps

filename = "example.csv"

col_names = ["translated_string", 
             "skill", "skill_startingPosition", "skill_endingPosition", 
             "title", "title_startingPosition", "title_endingPosition"]

df = pd.read_csv(filename, header=0, names=col_names)

def create_dicts(x):
    return x.iloc[:,[1,2,3]].to_dict("records"), x.iloc[:,[4,5,6]].to_dict("records")

df = (df.groupby(["translated_string"])
        .apply(create_dicts)
        .reset_index()
        .rename(columns={0:"nested"})
      )
df[["skills", "titles"]] = pd.DataFrame(df["nested"].to_list(), index=df.index)

json_file = df.drop(columns=["nested"]).to_json(orient="records")
json_file = re.sub(r"(skill_|title_)\W*", "", json_file)
json_file = dumps(loads(json_file), indent=4)

with open("output.json", "w") as f:
    f.write(json_file)

字符串

相关问题