【问题标题】:Data from csv to json [duplicate]从csv到json的数据[重复]
【发布时间】:2017-03-02 22:11:13
【问题描述】:

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

输入 (CSV)

"名字","姓氏","电子邮件","订单总数","总花费","平均 OrderValue","最后下单日期","客户自","Shipping 名称","收货地址 1","收货地址 2","收货地址 城市","送货省/州","送货邮编","送货 国家","送货电话号码","帐单名称","帐单地址 1","帐单地址 2","帐单城市","帐单 省/州","帐单邮编","帐单国家","帐单电话 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","测试","测试 测试","","测试","","测试","测试","","测试","测试 测试","","测试","","测试","测试","",""

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

这就是我想要得到的,但我什至还没有接近。我只能获取所有数据,我不知道如何将其过滤掉,并保持部分不变。

【问题讨论】:

  • 那么您尝试过什么,具体问题在哪里。向我们展示您的代码。
  • 你必须展示你的尝试,你不能只寻求一般帮助。
  • 你能把 CSV 文件上传到某个地方吗?

标签: python json csv


【解决方案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"
            }
        ]
    }
}

【讨论】:

  • 非常感谢,这正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 2020-09-08
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
相关资源
最近更新 更多