2017-09-25 114 views
-2

我已收集的數據爲csv文件和數據的模樣:轉換.csv文件到上傳.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 

第一行包含列名和行的其餘部分是數據。

我怎樣才能將數據轉換成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

[Python CSV to JSON]的可能重複(https://stackoverflow.com/questions/19697846/pytho n-csv-to-json) –

+0

[csv to json conversion](https://stackoverflow.com/q/38170071/2823755)。 – wwii

回答

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('}')