非常感谢,我一直热衷于收集一些关于从相应输入中获取以下输出的见解。希望通过使用 python 脚本将表格转换为所需的格式,因为我必须在稍后阶段使用巨大的 CSV。任何输入都受到高度赞赏。
输入 CSV:
参考 | 微控制器 | 价值 | 货币 |
---|---|---|---|
10000 | 5300 | 134.09 | 美元 |
10001 | 5651 | 128.95 | 美元 |
10002 | 5912 | 104.71 | 美元 |
使用的python代码:
from csv import DictReader
from itertools import groupby
from pprint import pprint
import json
with open('Test_bulk_transactions_data.csv') as csvfile:
r = DictReader(csvfile, skipinitialspace=True)
data = [dict(d) for d in r]
group = []
uniquekeys = []
for k, g in groupby(data, lambda r: (r['reference'], r['mcc'])):
group.append({
"reference": k[0],
"mcc": k[1],
"amount": [{k:v for k, v in d.items() if k not in ['reference','mcc']} for d in list(g)]})
uniquekeys.append(k)
print(json.dumps(group, indent = 3) + '}')
电流输出:
{
"reference": "10000",
"mcc": "5300",
"amount": [
{
"value": "134.09",
"currency": "USD"
}
]
},
{
"reference": "10001",
"mcc": "5651",
"amount": [
{
"value": "128.95",
"currency": "USD"
}
]
},
{
"reference": "10002",
"mcc": "5912",
"amount": [
{
"value": "104.71",
"currency": "USD"
}
]
}
所需的输出 JSON:
{
"reference": "10000",
"mcc": "5300",
"amount": {
"value": 134.09,
"currency": "USD"
}
},
{
"reference": "10001",
"mcc": "5651",
"amount": {
"value": 128.95,
"currency": "USD"
}
},
{
"reference": "10002",
"mcc": "5912",
"amount": {
"value": 104.71,
"currency": "USD"
}
}
- 重要说明:金额不应落入 [] 并且该值应作为数字输出而不是字符串。