简体   繁体   English

为每行csv创建一个JSON

[英]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). 我试图弄清楚如何调整我的代码,以便它将csv的每一行写入其自己的json,然后将其发布(以及我想尝试遍历的方式,每个json文件都被覆盖时间没问题)。

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. 我的代码会根据需要生成结构化的json,并将所有内容格式化为正确的数据类型,但是我一生无法解决如何明智地遍历此行。 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). 将您从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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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