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

在python中将.csv文件转换为.json格式,python,json,csv,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/383

我已将数据收集到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
{{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}}
第一行包含列名,其余行是数据

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

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
{{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)
表示行号

这可以使用Python完成,如下所示:

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('}') 
可能的副本。