为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Python 将 CSV 转换为嵌套 JSON

使用 Python 将 CSV 转换为嵌套 JSON

拉风的咖菲猫 2023-11-09 21:36:20
我需要将以下 CSV 数据解析为嵌套的 JSON 字符串。请告知我如何将“ payment_mode”添加为“cashier”的嵌套值。我尝试了一些方法,例如创建另一个orderedDict并将其附加到子集列表中,但这并没有按预期工作。将不胜感激任何帮助。CSV 数据:Contract_no,sales_date,store_sales_amount,cashier_counter,discount_amount,before_tax_amount,tax_amount,cashier_amount,product,dine_in,take_away,mode,amountCS,2020-04-12,18.50,C1,0,18.50,0,18.50,18.50,0,0,CASH,1068.50预期的 JSON 格式:    {    "contract_no": "CS",    "sales_date": "2020-04-06",    "store_sales_amount": "822.17",    "cashier": [        {            "cashier_counter": "C1",            "discount_amount": "15",            "before_tax_amount": "13.15",            "tax_amount": "219.13",            "cashier_amount": "232.28",          "product":"100.12",          "dine_in":"116.02",          "take_away":"16.14",            "payment_mode": [                {                    "mode": "CASH",                    "amount": "112.46"                }            ]        },    ]}电流输出:{"contract_no": "CS","sales_date": "2020-04-12","store_sales_amount": "18.50","cashier": [    {        "cashier_counter": "C1",        "discount_amount": "0",        "before_tax_amount": "18.50",        "tax_amount": "0",        "cashier_amount": "18.50",        "product": "18.50",        "dine_in": "0",        "take_away": "0",        "mode": "CASH",        "cash_amount": "18.50"    }]}代码import pandas as pdfrom itertools import groupby from collections import OrderedDictimport json    #read csv into dataframedf = pd.read_csv('sales2.csv', dtype={        #level1        "Contract_no" : str,        "sales_date" : str,        "store_sales_amount" : str,        #level2 cashier        "cashier_counter" : str,        "discount_amount" : str,        "before_tax_amount" : str,        "tax_amount" : str,        "cashier_amount" : str,        "product" : str,        "dine_in" : str,        "take_away" : str,        #level3 payment_mode        "mode" : str,        "cash_amount" : str             })    
查看完整描述

1 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

你是这个意思吗?


import json


csv = """Contract_no,sales_date,store_sales_amount,cashier_counter,discount_amount,before_tax_amount,tax_amount,cashier_amount,product,dine_in,take_away,mode,amount

CS,2020-04-12,18.50,C1,0,18.50,0,18.50,18.50,0,0,CASH,1068.50"""


table = [row.split(",") for row in csv.split("\n")]


""""

  0             1            2                    3                 4                 5                   6            7                8         9         10          11     12

|-------------|------------|--------------------|-----------------|-----------------|-------------------|------------|----------------|---------|---------|-----------|------|---------|

| Contract_no | sales_date | store_sales_amount | cashier_counter | discount_amount | before_tax_amount | tax_amount | cashier_amount | product | dine_in | take_away | mode | amount  |

|-------------|------------|--------------------|-----------------|-----------------|-------------------|------------|----------------|---------|---------|-----------|------|---------|

| CS          | 2020-04-12 | 18.50              | C1              | 0               | 18.50             | 0          | 18.50          | 18.50   | 0       | 0         | CASH | 1068.50 |

|-------------|------------|--------------------|-----------------|-----------------|-------------------|------------|----------------|---------|---------|-----------|------|---------|

"""


jsn = {

    "contract_no":               table[1][0],

    "sales_date":                table[1][1],

    "store_sales_amount":        table[1][2],

    "cashier": [

        {

            "cashier_counter":   table[1][3],

            "discount_amount":   table[1][4],

            "before_tax_amount": table[1][5],

            "tax_amount":        table[1][6],

            "cashier_amount":    table[1][7],

            "product":           table[1][8],

            "dine_in":           table[1][9],

            "take_away":         table[1][10],

            "payment_mode": [

                {

                    "mode":      table[1][11],

                    "amount":    table[1][12]

                }

            ]

        },

    ]

}


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

输出:


{

    "contract_no": "CS",

    "sales_date": "2020-04-12",

    "store_sales_amount": "18.50",

    "cashier": [

        {

            "cashier_counter": "C1",

            "discount_amount": "0",

            "before_tax_amount": "18.50",

            "tax_amount": "0",

            "cashier_amount": "18.50",

            "product": "18.50",

            "dine_in": "0",

            "take_away": "0",

            "payment_mode": [

                {

                    "mode": "CASH",

                    "amount": "1068.50"

                }

            ]

        }

    ]

}

我不知道你的工作流程。也许jsn也可以这样定义:


jsn = {

    table[0][0]: table[1][0],

    table[0][1]: table[1][1],

    table[0][2]: table[1][2],

    "cashier": [

        {

            table[0][3]:  table[1][3],

            table[0][4]:  table[1][4],

            table[0][5]:  table[1][5],

            table[0][6]:  table[1][6],

            table[0][7]:  table[1][7],

            table[0][8]:  table[1][8],

            table[0][9]:  table[1][9],

            table[0][10]: table[1][10],

            "payment_mode": [

                {

                    table[0][11]: table[1][11],

                    table[0][12]: table[1][12]

                }

            ]

        },

    ]

}


查看完整回答
反对 回复 2023-11-09
  • 1 回答
  • 0 关注
  • 56 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信