简体   繁体   English

格式化csv数据并将每一行写入json

[英]Format csv data and write each row to a json

I'm trying to write each row of a csv to a json (this will then be posted and looped back through so overwriting the json file is not a big deal here). 我正在尝试将csv的每一行写入json(这将被发布并循环回来,因此覆盖json文件在这里不是什么大问题)。 I have code which seems to do this well enough, but also need to some of the data to be floats/integers rather than strings. 我有足够的代码,但也需要一些数据浮点数/整数而不是字符串。

I have a method which works for this in other places, but cannot manage to get the two to agree with each other. 我有一种方法可以在其他地方使用,但无法让两者相互认同。

Could anyone point me in the right direction to be able to format the csv data before sending it out as a json? 任何人都可以指出我在正确的方向上能够格式化csv数据,然后将其作为json发送出去吗? Below is the code for when headers are left in, though I also have a tweaked version which just has raw data in the csv and uses fieldnames for the headers instead. 下面是标题留下的代码,虽然我也有一个调整版本,它只是在csv中有原始数据,而是使用头文件的字段名。

import csv
import json

input_file = 'Test3.csv'
output_file_template = 'Test.json'

with open(input_file, 'r', encoding='utf8') as csvfile:
    reader = csv.DictReader(csvfile, delimiter=',')
    rows = list(reader)

for i in range(len(rows)):
    out = json.dumps(rows[1*i:1*(i+1)])
    with open(output_file_template.format(i), 'w') as f:
        f.write(out)

Data is in a format like this: 数据格式如下:

OrderType OrderStatus OrderDateTime SettlementDate MarketId OrderRoute OrderType OrderStatus OrderDateTime SettlementDate MarketId OrderRoute

Sale Executed 18/11/2016 23/11/2016 1 None 促销销售18/11/2016 23/11/2016 1无

Sale Executed 18/11/2016 23/11/2016 1 None 促销销售18/11/2016 23/11/2016 1无

Sale Executed 18/11/2016 23/11/2016 1 None 促销销售18/11/2016 23/11/2016 1无

With row[4] producing the key error. 行[4]产生键错误。

In your loop if the float/int data is consistently in the same spot, you can simply cast the values. 在循环中,如果float / int数据始终位于同一位置,则可以简单地转换值。

for i, row in enumerate(rows):
    row[0] = int(row[0]) # this column stores ints
    row[1] = float(row[1]) # this column stores floats
    out = json.dumps([row])
    with open(output_file_template.format(i), 'w') as f:
        f.write(out)

I don't know if columns 0 and 1 hold ints and floats, but you can change that as necessary. 我不知道列0和1是否包含整数和浮点数,但您可以根据需要进行更改。

Update: 更新:

It appears row is an OrderedDict , so you'll just need to use the key instead of an index: 看来row是一个OrderedDict ,所以你只需要使用键而不是索引:

row['MarketId'] = int(row['MarketId'])

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

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