使用 python 以所需格式从 CSV 中检索 JSON

Retrieve JSON from CSV using python in desired format

非常感谢,我一直热衷于收集一些关于从相应输入中获取以下输出的见解。希望通过使用 python 脚本将 table 转换为理想的格式,因为我必须在稍后阶段使用巨大的 CSV。非常感谢任何意见。

输入 CSV:

reference mcc value currency
10000 5300 134.09 USD
10001 5651 128.95 USD
10002 5912 104.71 USD

使用的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"
    }
  }
import csv

csv_filepath =  "/home/mhs/test.csv"
 
output = []


with open(csv_filepath) as cd:
    csvReader = csv.DictReader(cd)
    for r in csvReader:
        r["amount"] = {"value": float(r.pop("value")), "currency": r.pop("currency")}
        output.append(r)

在不依赖导入模块的情况下,您可以这样做:

J = []

with open('input.csv') as csv:
    cols = next(csv).split()
    assert len(cols) == 4
    for row in csv:
        t = row.split()
        if len(t) == 4:
            J.append({cols[0]: t[0], cols[1]: t[1], "amount": {cols[2]: float(t[2]), cols[3]: t[3]}})


print(J)