I'm trying to create a program that converts a CSV file into a JSON file with a specific layout.
Input (CSV)
"First Name","Last Name","Email","Total Orders","Total Spent","Average OrderValue","Date of Last Order","Customer Since","Shipping Name","Shipping Address 1","Shipping Address 2","Shipping City","Shipping Province/State","Shipping Zip","Shipping Country","Shipping Phone Number","Billing Name","Billing Address 1","Billing Address 2","Billing City","Billing Province/State","Billing Zip","Billing Country","Billing Phone Number","Tags""test","test","test@hotmail.com","1","19.95","19.95","2016-10-06 11:48:02 -0400","2016-10-06 11:48:02 -0400","test","test test","","test","","test","test","","test","test test","","test","","test","test","",""
Output (JSON)
POST https://api.myparcel.nl/shipments HTTP/1.1
Content-Type:application/vnd.shipment+json;charset=utf-8 Authorization:basic SmVzdXNDaHJpc3Rpc0xvcmQ3Nzc=
{
"data":{
"shipments":[
{
"recipient":{
"cc":"test",
"city":"test",
"street":"test",
"number":"test",
"postal_code":"test",
"person":"test",
"phone":"",
"email":"test@hotmail.com"
},
"options":{
"package_type":1,
"only_recipient":1,
"signature":1,
"return":1,
"insurance":{
"amount":50000,
"currency":"EUR"
},
"large_format":0
},
"carrier":1
},
{
"recipient":{
"cc":"test",
"city":"test",
"street":"test",
"number":"test",
"postal_code":"test",
"person":"test",
"phone":"",
"email":"test@hotmail.com"
},
"options":{
“package_type”:1,
"only_recipient":0,
"signature":0,
"return":0
},
"carrier":1
}
]
}
}
this is what I'm trying to get, but I'm not even close. I can only get all the data in and I don't know how to filter it out, and to keep a part unchanged.
答案 0 :(得分:1)
也许这会对你有帮助。
import json
import csv
csvfile = open('a.csv', 'r')
reader = csv.reader(csvfile, delimiter=',')
reader = csv.DictReader(csvfile, next(reader))
result = {'data': {}}
for row in reader:
shipping = {}
billing = {}
customer = {}
for key, value in row.iteritems():
if 'Shipping' in key:
shipping[key] = value
elif 'Billing' in key:
billing[key] = value
else:
customer[key] = value
result['data']['Customer'] = [customer]
result['data']['Shipping'] = [shipping]
result['data']['Billing'] = [billing]
print json.dumps(result, sort_keys=True, indent=4)
结果:
{
"data": {
"Billing": [
{
"Billing Address 1": "test test",
"Billing Address 2": "",
"Billing City": "test",
"Billing Country": "test",
"Billing Name": "test",
"Billing Phone Number": "",
"Billing Province/State": "",
"Billing Zip": "test"
}
],
"Customer": [
{
"Average OrderValue": "19.95",
"Customer Since": "2016-10-06 11:48",
"Date of Last Order": "2016-10-06 11:48",
"Email": "test@hotmail.com",
"First Name": "test",
"Last Name": "test",
"Tags": "",
"Total Orders": "1",
"Total Spent": "19.95"
}
],
"Shipping": [
{
"Shipping Address 1": "test test",
"Shipping Address 2": "",
"Shipping City": "test",
"Shipping Country": "test",
"Shipping Name": "test",
"Shipping Phone Number": "",
"Shipping Province/State": "",
"Shipping Zip": "test"
}
]
}
}