【问题标题】:Dataframe object in conversion from xlsx to JSON从 xlsx 转换为 JSON 的数据框对象
【发布时间】:2020-03-25 07:48:00
【问题描述】:

我是 Python 编程新手,我正在尝试编写一个读取 xlxs 文件并将其转换为 json 的程序。 (我一直在使用 Python 3 和 Jupyter Notebook,但我遇到了一些问题和困难。)

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

我的 xlsx 文件中有两行四列:

id     label        id_customer     label_customer

6     Sao Paulo      CUST-99992         Brazil

92    Hong Hong      CUST-88888         China

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

这是我的代码:

import pandas as pd
import json

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

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

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

print (list)

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

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

Json 输出:

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

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

预期结果:

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

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

谁能帮帮我?

【问题讨论】:

    标签: python json excel pandas jupyter-notebook


    【解决方案1】:

    对您的代码进行了一些测试,我认为您仅获得单行值的问题可能是由于您的代码输入存在问题。我已经使用您的数据作为 .csv 输入运行了它,它的输出似乎与您最终显示的不同。我不确定您使用的是什么版本的 pandas,但我建议您使用 file_imported 运行一些测试以确认它具有正确的值。我为此使用了file_imported = pd.read_csv('import.csv')

    我还发现了其他一些您可能会遇到的其他相关问题。当输入良好时,这是我得到的输出:

    [
     {
      "id": 6,
      "label": "S\u00e3o Paulo",
      "Customer": [
       {
        "id": "CUST-99992",
        "label": "Brazil"
       },
       {
        "id": "CUST-88888",
        "label": "China"
       }
      ]
     },
     {
      "id": 92,
      "label": "Hong Hong",
      "Customer": [
       {
        "id": "CUST-99992",
        "label": "Brazil"
       },
       {
        "id": "CUST-88888",
        "label": "China"
       }
      ]
     }
    ]
    

    解决这个问题很简单,只需将 [index] 添加到 "Customer" : list1 引用的末尾,如下所示:`"Customer" : list1[index]。通过这两个更改,这就是您应该得到的输出!

    [
     {
      "id": 6,
      "label": "S\u00e3o Paulo",
      "Customer": {
       "id": "CUST-99992",
       "label": "Brazil"
      }
     },
     {
      "id": 92,
      "label": "Hong Hong",
      "Customer": {
       "id": "CUST-88888",
       "label": "China"
      }
     }
    ]
    

    希望这会有所帮助!

    【讨论】:

    • 我升级了我的熊猫,现在我使用的是 0.23.0 版本!!我检查了我提到的输出,因为我在'for'循环中错误地添加了'list'和'list1'(我已经修复了它)。我可以将 '[index]' 添加到 "Customer" 的末尾:list1 吗? list1 是一个列表,应该以'['而不是'{'开头..
    • 绝对!这正是应该解决此问题的方法,因为您只想从每次迭代中引用 list1 的特定值。在循环中,列表应该像这样引用列表 1:"Customer" : list1[index]
    【解决方案2】:

    在将 list1 添加到列表之前尝试附加它

    import pandas as pd
    import json
    
    file_imported = pd.read_excel('testing.xlsx', sheet_name = 'Plan1')
    
    list1 = []
    list  = []
    for index, row in file_imported.iterrows():
        list1.append ({
               "id"       : str(row['id_customer']) ,
               "label"    : str(row['label_customer'])
               })
        list.append ({
                "id"       : int(row['id']),
                "label"    : str(row['label']),
                "Customer" : list1
                })
    
    
    
        print (list)
    
    with open ('testing.json', 'w') as f:
         json.dump(list, f, indent= True)
    

    【讨论】:

    • 我尝试先附加 list1,但没有成功...我编辑了我的问题..我希望这有助于更好地理解问题..
    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2021-06-20
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2021-12-21
    相关资源
    最近更新 更多