簡體   English   中英

使用 Pandas 將 CSV 轉換為嵌套的 JSON 復雜結構

[英]Convert CSV to Nested JSON complex structure using Pandas

使用 Pandas 轉換為嵌套的 JSON 文件

這是一行的示例 csv

name      type  aitm      alitm     aaitm           adsc1   
specs     glass 70072187  ESA65Z45  ESA 65Z45       CUT TIP FG 1808-40  

我正在嘗試為每一行實現以下嵌套 JSON 結構

import pandas as pd
import json

df = pd.DataFrame([['specs','glass','70072187','ESA65Z45','ESA 65Z45','CUT TIP FG 1808-40'],
                   ['specs','glass','666','ESA6665','ESB 666','CUT TIP FG 66-40']],
                       columns = ['name', 'type','aitm','alitm','aaitm','adsc1' ])


data = {'entities':[]}
for key,grp in df.groupby('name'):
    for idx, row in grp.iterrows():
        temp_dict_alpha = {'name':key, 'type':row['type'], 'data':{'attributes':{}}}

        attr_row = row[~row.index.isin(['name','type'])]
        for idx2, row2 in attr_row.iteritems():
            dict_temp = {}
            dict_temp[idx2] = {'values':[]}
            dict_temp[idx2]['values'].append({'value':row2,'source':'internal','locale':'en_US'})

            temp_dict_alpha['data']['attributes'].update(dict_temp)

        data['entities'].append(temp_dict_alpha)


print(json.dumps(data, indent= 4))   

輸出:

print(json.dumps(data, indent= 4))
{
    "entities": [
        {
            "name": "specs",
            "type": "glass",
            "data": {
                "attributes": {
                    "aitm": {
                        "values": [
                            {
                                "value": "70072187",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "alitm": {
                        "values": [
                            {
                                "value": "ESA65Z45",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "aaitm": {
                        "values": [
                            {
                                "value": "ESA 65Z45",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "adsc1": {
                        "values": [
                            {
                                "value": "CUT TIP FG 1808-40",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    }
                }
            }
        },
        {
            "name": "specs",
            "type": "glass",
            "data": {
                "attributes": {
                    "aitm": {
                        "values": [
                            {
                                "value": "666",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "alitm": {
                        "values": [
                            {
                                "value": "ESA6665",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "aaitm": {
                        "values": [
                            {
                                "value": "ESB 666",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "adsc1": {
                        "values": [
                            {
                                "value": "CUT TIP FG 66-40",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    }
                }
            }
        }
    ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM