-2

I have collected data into a csv file and data looks like:

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

The first row contains column names and rest of rows is the data.

How can I convert the data into json file which looks like:

{{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}}

Note: The numbers 1), 2), 3) represents row numbers

2

1 Answer 1

1

This can be done using a Python csv.DictReader() as follows:

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('}') 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.