数据从csv到json

2024-04-16 20:37:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个程序,将CSV文件转换为具有特定布局的JSON文件。你知道吗

输入(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","",""

输出(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
         }
      ]
   }
}

这就是我想得到的,但我还没接近。我只能得到所有的数据,我不知道如何过滤掉它,并保持一部分不变。你知道吗


Tags: 文件csvnametestcomjsoncityaddress
1条回答
网友
1楼 · 发布于 2024-04-16 20:37:25

也许这对你有帮助。你知道吗

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"
            }
        ]
    }
}

相关问题 更多 >