简体   繁体   中英

Create a JSON per row of csv

I'm trying to figure out how to tweak my code so that it will write each row of a csv to its own json, which will then be posted (with how I want to try and loop through this, the json file being overwritten each time is no issue).

My code produces structured jsons as I need it to and formats everything to the correct data type, but I cannot for the life of me figure out how to loop through this row wise. I have another piece of code which can achieve this second aim, but all my attempts at combining them have failed so far.

Any suggestions of how I might loop through this code?

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)

Convert each dict that you create from a CSV row to JSON, and write that to a file (or POST it to a URL, or whatever you want to do with it).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM