如何将 CSV 转换为 Keyed JSON?

车探cc

目前,我正在使用以下代码创建 JSON:

import pandas as pd
import csv
data = pd.read_csv(r'C:\Users\user\Downloads\config_all_env.csv')
# print(data.to_string())
df = pd.DataFrame(data)
# print(df)

json_file = df.to_json(orient='index',indent=4)
print(json_file)

我的“config_all_env.csv”看起来像这样

_key,index,source
123456789100:LogGroup1,index1,Account1-123456789100:LogGroup1
123456789100:LogGroup2,index1,Account1-123456789100:LogGroup2
678901234200:LogGroup3,index2,Account2-678901234200:LogGroup3
678901234200:LogGroup4,index2,Account2-678901234200:LogGroup4

输出是这样的:

{
    "0":{
        "_key":"123456789100:LogGroup1",
        "index":"index1",
        "source":"Account1-123456789100:LogGroup1"
    },
    "1":{
        "_key":"123456789100:LogGroup2",
        "index":"index1",
        "source":"Account1-123456789100:LogGroup2"
    },
    "2":{
        "_key":"678901234200:LogGroup3",
        "index":"index2",
        "source":"Account2-678901234200:LogGroup3"
    },
    "3":{
        "_key":"678901234200:LogGroup4",
        "index":"index2",
        "source":"Account2-678901234200:LogGroup4"
    }
}

但是,这个网站:https : //www.convertcsv.com/csv-to-json.htm有一个 Keyed JSON 选项,在解析后收到这个输出:

{
   "123456789100:LogGroup1": {
      "index": "index1",
      "source": "Account1-123456789100:LogGroup1"
   },
   "123456789100:LogGroup2": {
      "index": "index1",
      "source": "Account1-123456789100:LogGroup2"
   },
   "678901234200:LogGroup3": {
      "index": "index2",
      "source": "Account2-678901234200:LogGroup3"
   },
   "678901234200:LogGroup4": {
      "index": "index2",
      "source": "Account2-678901234200:LogGroup4"
   }
}

如何使用 Python 实现这一点,因为每次都打开这个网站并进行转换是不可行的?

闪闪发光

如果您首先将“_key”列设置为熊猫数据框的索引,这应该可以实现您想要的(如果我正确解释了您的问题)。

import pandas as pd

my_data = pd.DataFrame.from_dict({
    "0":{
        "_key":"123456789100:LogGroup1",
        "index":"index1",
        "source":"Account1-123456789100:LogGroup1"
    },
    "1":{
        "_key":"123456789100:LogGroup2",
        "index":"index1",
        "source":"Account1-123456789100:LogGroup2"
    },
    "2":{
        "_key":"678901234200:LogGroup3",
        "index":"index2",
        "source":"Account2-678901234200:LogGroup3"
    },
    "3":{
        "_key":"678901234200:LogGroup4",
        "index":"index2",
        "source":"Account2-678901234200:LogGroup4"
    }
}, orient="index")

my_dict = my_data.set_index("_key").to_json(orient='index',indent=4)

输出:

{
    "123456789100:LogGroup1":{
        "index":"index1",
        "source":"Account1-123456789100:LogGroup1"
    },
    "123456789100:LogGroup2":{
        "index":"index1",
        "source":"Account1-123456789100:LogGroup2"
    },
    "678901234200:LogGroup3":{
        "index":"index2",
        "source":"Account2-678901234200:LogGroup3"
    },
    "678901234200:LogGroup4":{
        "index":"index2",
        "source":"Account2-678901234200:LogGroup4"
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章