2016-10-20 14 views
0

CSVファイルを特定のレイアウトのJSONファイルに変換するプログラムを作成しようとしています。csvからjsonへのデータ

入力(CSV)

"姓"、 "姓"、 "電子メール"、 "トータル受注"、 "合計使用済"、 "平均 OrderValue"、 "ラストオーダーの日" 「 国送料」、「 名前送料」、「配送先住所1」、「配送先住所2」、「 市送料」、「送料省/州」、「送料ジップ」、「以来お客様」、「配送 省/州課金都市課金名を課金電話番号「 『』、 『請求先住所 1』、 『請求先住所2』、 『』、 『』、 『請求ジップ』、 『請求先の国』、」課金電話 番号"、"タグ ""テスト "、"テスト "、"テスト@ホットメール。 「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":"[email protected]" 
      }, 
      "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":"[email protected]" 
      }, 
      "options":{ 
       “package_type”:1, 
       "only_recipient":0, 
       "signature":0, 
       "return":0 
      }, 
      "carrier":1 
     } 
     ] 
    } 
} 

これは私が取得しようとしているが、私は似ても似つかないんだものです。私はすべてのデータを取り込むことしかできず、フィルタリングする方法や部品を変更しないようにする方法がわかりません。

+0

だからあなたは何を試してみました、どこ正確な問題です。あなたのコードを私たちに教えてください。 – UnholySheep

+0

あなたはあなたが試みたことを示す必要があります、あなたは一般的な助けを求めることはできません。 – bravosierra99

+0

CSVファイルをどこかにアップロードできますか? – kyle

答えて

0

多分、これはあなたを助けるでしょう。

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": "[email protected]", 
       "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" 
      } 
     ] 
    } 
} 
+0

ありがとう、これは私が探していたものです。 – paulcodes10

関連する問題