在python中将.csv文件转换为.json格式

时间:2017-09-25 17:45:26

标签: python json csv

我已将数据收集到csv文件中,数据如下:

1)username;date;retweets;favorites;text;geo;mentions;hashtags;id;permalink
2)CFSharing;2017-05-27 01:59;0;0;"£5 million raised for Manchester bombing victims in just three days #love http:// crowdfunding.einnews.com/article/383427 730/5ppRHGKOfQkNtAFM?ref=rss&ecode=SQUvWbWCp_PWzTRB …";;;#love;"868255418203557888";https://twitter.com/CFSharing/status/868255418203557888
3)TracksuitDavid;2017-05-27 01:58;1;0;"Tony Blair has been eerily silent on the Manchester bombing . For one very good reason. https://www. thecanary.co/2017/05/26/ton y-blair-eerily-silent-manchester-bombing-one-good-reason/ … via @thecanarysays";;@thecanarysays;;"868254940548001792";https://twitter.com/TracksuitDavid/status/868254940548001792

第一行包含列名,其余行包含数据。

如何将数据转换为json文件,如下所示:

{{username:CFSharing,date:2017-05-27 01:59,retweets:0,favorites:0,text:,geo:,mentions:,hashtags:,id:"868255418203557888",permalink:https://twitter.com/CFSharing/status/868255418203557888},
{username:TracksuitDavid,date:2017-05-27 01:58;1;0,retweets:,favorites:,text:"Tony Blair has been eerily silent on the Manchester bombing . For one very good reason. https://www. thecanary.co/2017/05/26/ton y-blair-eerily-silent-manchester-bombing-one-good-reason/ … via @thecanarysays",geo:,mentions:@thecanarysays,hashtags:,id:"868254940548001792",permalink:https://twitter.com/TracksuitDavid/status/868254940548001792}}

注意:数字1)2)3)代表行号

1 个答案:

答案 0 :(得分:1)

这可以使用Python csv.DictReader()完成,如下所示:

import csv
import json

with open('input.csv', 'rb') as f_input, open('output.json', 'w') as f_output:
    first = True

    for row in csv.DictReader(f_input, delimiter=';'):
        if first:
            f_output.write('{')
            first = False
        else:
            f_output.write(',\n')
        json.dump(row, f_output)

    f_output.write('}')