2017-10-16 55 views
1

我想弄清楚如何調整我的代碼,以便它會CSV的每一行寫入其自己的JSON,然後將被張貼(用我多麼想嘗試的一個排和JSON通過這個循環中,JSON文件被覆蓋每次都是沒有問題)。創建每CSV

我的代碼生成,因爲我需要它的結構jsons並格式化一切正確的數據類型,但我不能爲我的生活弄清楚如何遍歷該行明智。我有另一段代碼可以實現這個第二個目標,但到目前爲止,我所有的努力在結合他們都失敗了。

我怎麼可能通過這個代碼迴路的任何建議?

output = [] 
with open('Test3.csv') as csv_file: 
    for a in csv.DictReader(csv_file): 
     output.append({ 
      'OrderType': a['OrderType'], 
      'OrderStatus': a['OrderStatus'], 
      'OrderDateTime': a['OrderDateTime'], 
      'SettlementDate': a['SettlementDate'], 
      'MarketId': int(a['MarketId']), 
      'OrderRoute': a['OrderRoute'], 
      'OrderEntityType': a['OrderEntityType'], 
      'SecurityId': a['SecurityId'], 
      'CurrencyISOCode': a['CurrencyISOCode'], 
      'Price': float(a['Price']), 
      'TotalCommission': float(a['TotalCommission']), 
      'SettlementStatus': a['SettlementStatus'], 
      'QuantitySettled': float(a['QuantitySettled']), 
      'SecurityOrderAllocations': { 
       'Reference': a['Account Number'], 
       'InvestmentCollectiveId': a['Account Number'], 
       'NominalAmount': float(a['QuantitySettled']), 
       'InvestmentAmount': float(a['InvestmentAmount']), 
       'OpenNominal': float(a['QuantitySettled']), 
       'SettlementCurrencyISOCode': 'USD', 
       'SettlementAccountId': a['Account Number'], 
       'OrderToSettlementExchangeRate': float('1'), 
       'SettlementToPortfolioExchangeRate': float('1'), 
       'OrderToPortfolioExchangeRate': float('1') 
      } 
     }) 

output_json = json.dumps(output) 
with open ('Test.json', 'w') as f: 
    f.write(output_json) 
+0

你想每一行被寫入到不同的JSON文件,如'test1.json','test2.json'等? – Barmar

+0

爲了澄清 - 是你的目標必須保存爲自己的JSON文件中的每個(目前附加)字典? – DZack

+0

@Barmar我很高興將這些文件寫入單個文件,或者將它們寫入一個文件,這些文件在發佈之後會不斷被覆蓋 - 此代碼目前已設置爲在一次調用中寫入一個非常長的json,而不是單獨。 – Bleu

回答

2

相互轉換,你從CSV行JSON創建,並寫一個文件(或郵寄到一個URL,或任何你想用它做)字典。

filenum = 1 
with open('Test3.csv') as csv_file: 
    for a in csv.DictReader(csv_file): 
     json = json.dumps({ 
      'OrderType': a['OrderType'], 
      'OrderStatus': a['OrderStatus'], 
      'OrderDateTime': a['OrderDateTime'], 
      'SettlementDate': a['SettlementDate'], 
      'MarketId': int(a['MarketId']), 
      'OrderRoute': a['OrderRoute'], 
      'OrderEntityType': a['OrderEntityType'], 
      'SecurityId': a['SecurityId'], 
      'CurrencyISOCode': a['CurrencyISOCode'], 
      'Price': float(a['Price']), 
      'TotalCommission': float(a['TotalCommission']), 
      'SettlementStatus': a['SettlementStatus'], 
      'QuantitySettled': float(a['QuantitySettled']), 
      'SecurityOrderAllocations': { 
       'Reference': a['Account Number'], 
       'InvestmentCollectiveId': a['Account Number'], 
       'NominalAmount': float(a['QuantitySettled']), 
       'InvestmentAmount': float(a['InvestmentAmount']), 
       'OpenNominal': float(a['QuantitySettled']), 
       'SettlementCurrencyISOCode': 'USD', 
       'SettlementAccountId': a['Account Number'], 
       'OrderToSettlementExchangeRate': float('1'), 
       'SettlementToPortfolioExchangeRate': float('1'), 
       'OrderToPortfolioExchangeRate': float('1') 
      } 
     }) 
     with open('Test' + str(filenum) + '.csv', 'w') as f: 
      f.write(json) 
    filenum += 1