第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python 將 CSV 轉(zhuǎn)換為嵌套 JSON

使用 Python 將 CSV 轉(zhuǎn)換為嵌套 JSON

拉風(fēng)的咖菲貓 2023-11-09 21:36:20
我需要將以下 CSV 數(shù)據(jù)解析為嵌套的 JSON 字符串。請告知我如何將“ payment_mode”添加為“cashier”的嵌套值。我嘗試了一些方法,例如創(chuàng)建另一個orderedDict并將其附加到子集列表中,但這并沒有按預(yù)期工作。將不勝感激任何幫助。CSV 數(shù)據(jù):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預(yù)期的 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貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超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]

                }

            ]

        },

    ]

}


查看完整回答
反對 回復(fù) 2023-11-09
  • 1 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號