我已经根据csv文件中的可用数据创建了json请求表单。
示例输入文件:
Id,LineNo,Amt,ReceivedDt,FromDt,ToDate,regionId
123545,1,1000.00,2019-02-01T00:00:00,2019-02-01T00:00:00,2019-02-01T00:00:00,WA12
123545,2,200.00,2019-02-01T00:00:00,2019-02-01T00:00:00,2019-02-01T00:00:00,WA12
123545,3,200.00,2019-02-01T00:00:00,2019-02-01T00:00:00,2019-02-01T00:00:00,WA12
123546,1,200.00,2019-02-01T00:00:00,2019-02-01T00:00:00,2019-02-01T00:00:00,WA13
123546,2,200.00,2019-02-01T00:00:00,2019-02-01T00:00:00,2019-02-01T00:00:00,WA13
之前的要求是实现以下布局:
{
"data": [
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"regionId": "WA12",
"Id": "123545",
"LineNo": "1",
"Amt": "1000.00",
"FromDt": "2019-02-01T00:00:00"
},
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"regionId": "WA12",
"Id": "123545",
"LineNo": "2",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
},
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"regionId": "WA12",
"Id": "123545",
"LineNo": "3",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
}
]
},
{
"data": [
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"regionId": "WA13",
"Id": "123546",
"LineNo": "1",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
},
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"regionId": "WA13",
"Id": "123546",
"LineNo": "2",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
}
]
}
我可以使用下面的代码来实现它:
代码:
导入csv 导入json 导入itertools 导入运算符
with open('Test.csv') as f:
cf = csv.DictReader(f)
output = [{'data': list(rows)}
for id_, rows in itertools.groupby(cf, key=operator.itemgetter('Id'))]
data = json.dumps(output, indent=4)
print(data)
但是现在更改了,regionID必须分开。我不确定如何只选择与ID链接的regionId的一个值并将其传递到列表中。请注意下面regionID位置的变化。另外,请告诉我是否还有其他方法可以实现所需的布局。
所需的输出:
{
"data": [
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"Id": "123545",
"LineNo": "1",
"Amt": "1000.00",
"FromDt": "2019-02-01T00:00:00"
},
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"Id": "123545",
"LineNo": "2",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
},
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"Id": "123545",
"LineNo": "3",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
}
],
"regionId": "WA12"
},
{
"data": [
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"Id": "123546",
"LineNo": "1",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
},
{
"ToDate": "2019-02-01T00:00:00",
"ReceivedDt": "2019-02-01T00:00:00",
"Id": "123546",
"LineNo": "2",
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00"
}
],
"regionId": "WA13"
}
答案 0 :(得分:0)
将其弹出行并将其附加到外部字典:
with open('Test.csv') as f:
cf = csv.DictReader(f)
output = []
for _, rows in itertools.groupby(cf, key=operator.itemgetter('Id')):
region_id = None
for row in rows:
try:
region_id = row.pop('regionId')
except IndexError, KeyError:
pass
output.append({'data': list(rows), 'regionId': region_id})
答案 1 :(得分:0)
In[2]: import collections
...: import csv
...:
...: by_region = collections.defaultdict(list)
...: with open('infile.csv', 'r') as f:
...: reader = csv.DictReader(f)
...: for row in reader:
...: region = row.pop('regionId')
...: by_region[region].append(row)
...:
...: result = []
...: for key, value in by_region.items():
...: result.append({
...: 'data': value,
...: 'regionId': key
...: })
...:
In[3]: import json; print(json.dumps(result, indent=2))
[
{
"data": [
{
"Id": "123545",
"LineNo": "1",
"Amt": "1000.00",
"ReceivedDt": "2019-02-01T00:00:00",
"FromDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00"
},
{
"Id": "123545",
"LineNo": "2",
"Amt": "200.00",
"ReceivedDt": "2019-02-01T00:00:00",
"FromDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00"
},
{
"Id": "123545",
"LineNo": "3",
"Amt": "200.00",
"ReceivedDt": "2019-02-01T00:00:00",
"FromDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00"
}
],
"regionId": "WA12"
},
{
"data": [
{
"Id": "123546",
"LineNo": "1",
"Amt": "200.00",
"ReceivedDt": "2019-02-01T00:00:00",
"FromDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00"
},
{
"Id": "123546",
"LineNo": "2",
"Amt": "200.00",
"ReceivedDt": "2019-02-01T00:00:00",
"FromDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00"
}
],
"regionId": "WA13"
}
]