[英]Python csv to json using pandas - csv columns to nested json
[英]CSV to nested JSON using Python
我需要將以下 CSV 數據解析為嵌套的 JSON 字符串。 請告知我將如何添加“payment_mode”作為“收銀員”的嵌套值。 我已經嘗試了一些事情,比如創建另一個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,amount
CS,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 pd
from itertools import groupby
from collections import OrderedDict
import json
#read csv into dataframe
df = 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
})
results = []
for (Contract_no, sales_date, store_sales_amount), bag in df.groupby(["Contract_no", "sales_date", "store_sales_amount"]):
#remove 3 variables from array
contents_df = bag.drop(["Contract_no", "sales_date","store_sales_amount"], axis=1)
for (mode, cash_amount), bag2 in contents_df.groupby(["mode", "cash_amount"]):
subset = [OrderedDict(row) for i,row in contents_df.iterrows()]
results.append(OrderedDict([("Contract_no", Contract_no),
("sales_date", sales_date),
("store_sales_amount", store_sales_amount),
("cashier", subset)]))
print (json.dumps(results[0], indent=4))
#with open('ExpectedJsonFile.json', 'w') as outfile:
#outfile.write(json.dumps(results[0], indent=4))
你是這個意思嗎?
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]
}
]
},
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.