在列表中追加元素以转换为JSON

时间:2019-12-03 14:36:12

标签: python json pandas dataframe

我正在编写一个读取xlxs文件并将其转换为JSON的工具。我正在使用python 3和0.23.0版本的pandas。 以下是我的代码从xlxs中读取的数据:

id     label        id_customer     label_customer    part_number 

6     Sao Paulo      CUST-99992         Brazil          7897

6     Sao Paulo      CUST-99992         Brazil          1437

92    Hong Hong      CUST-88888         China           785

=================================

这是我的代码:

import pandas as pd
import json

file_imported = pd.read_excel('testing.xlsx', sheet_name = 'Plan1')

list_final  = []
for index, row in file_imported.iterrows():
    list1 = []
    list_final.append ({
        "id"       : int(row['id']),
        "label"    : str(row['label']),
        "Customer" : list1
        })

    list2 = []
    list1.append ({
       "id"       : str(row['id_customer']) ,
       "label"    : str(row['label_customer']),
       "number"   :  list2
       })

    list2.append({
        "part"    : str(row['part_number'])  
       })      

print (list_final)

with open ('testing.json', 'w') as f:
    json.dump(list_final, f, indent= True)

=================================

我的代码正在运行,这是我得到的输出:

[
 {
  "id": 6,
  "label": "Sao Paulo",
  "Customer": [
   {
    "id": "CUST-99992",
    "label": "Brazil",
    "number" : [
    {
    "part": "7897"
    }
    ]
   }
  ]
 },
 {
  "id": 6,
  "label": "Sao Paulo",
  "Customer": [
   {
    "id": "CUST-99992",
    "label": "Brazil",
    "number" : [
    {
    "part": "1437"
    }
    ]
   }
  ]
 },
 {
  "id": 92,
  "label": "Hong Hong",
  "Customer": [
   {
    "id": "CUST-88888",
    "label": "China",
    "number" : [
    {
    "part": "785"
    }
    ]
   }
  ]
 }
]   

=================================

,我需要这样的东西:

[
 {
  "id": 6,
  "label": "Sao Paulo",
  "Customer": [
   {
    "id": "CUST-99992",
    "label": "Brazil",
    "number" : [
    {
    "part": "7897"
    },
    {
    "part": "1437"
    }
    ]
   }
  ]
 },
 {
  "id": 92,
  "label": "Hong Hong",
  "Customer": [
   {
    "id": "CUST-88888",
    "label": "China",
    "number" : [
    {
    "part": "785"
    }
    ]
   }
  ]
 }
]

=================================

我一直在这里搜索其他主题或任何有用的材料,但尚未找到。这只是我的代码和excel文件的一部分(它们太大了,无法在此处发布)。我相信我必须先使用'if'语句来验证每一行中的内容,然后才能将其添加到json中,但是idk怎么做。

我可以在'list_final'内有很多具有不同内容的'客户'和'数字'列表(这就是为什么我那样创建Excel的原因)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

尝试此代码。我开始基于您的数据是熊猫数据框。

def part(value):
    data = value.split("#")
    part_list = []
    for elements in data:
        part_list.append({"part" : elements})
    return part_list


path = yourpath
data = pd.read_excel(path)
data["part_number"] = data["part_number"].apply(lambda x: str(x))
data = data.groupby(["id", "label", "id_customer", "label_customer"], as_index=False).agg("#".join)
data["part_number"] = data["part_number"].apply(lambda x: part(x))
data = data.rename(columns={"id_customer": "Customer", "part_number": "number"})
data["label_customer"] = data["label_customer"].apply(lambda x: {"label": x})
data["Customer"] = data["Customer"].apply(lambda x: {"id": x})
data["number"] = data["number"].apply(lambda x: {"number": x})
data["Customer"] = data.apply(lambda x: [{**x["Customer"], **x["label_customer"], **x["number"]}], axis=1)
data = data[["id", "label", "Customer"]]
data.to_json(path_you_want, orient="records")