1 回答
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]
}
]
},
]
}
添加回答
举报