next2billgates
next2billgates

Reputation: 1

Tweets Json File to Convert in csv

I have a complete dataset of tweets which i collect through Tweepy and save them as a json file. Now i want to Convert that data in csv file according to my need. Like only Text, Username, Created at and 4-5 more colums. How can i do this can any one please provide me a python code for this. and another problem is that on converting the data in csv my tweet text is also split where any comma comes. Please help us. I am a new in this field. Thanks in Advance.

json data

Upvotes: 0

Views: 2860

Answers (1)

Martin Evans
Martin Evans

Reputation: 46759

You would need to read your file in and convert each non-empty line from json format. You could then use itemgetter() to extract the required keys from the resulting dictionary and write the results to your output.csv file:

from operator import itemgetter        
import csv
import json

header = ['text', 'username', 'created_at']
required_cols = itemgetter(*header)

with open('python1.json') as f_input, open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(header)

    for row in f_input:
        if row.strip():
            csv_output.writerow(required_cols(json.loads(row)))

If you are using Python 3.x, use the following line:

with open('python1.json') as f_input, open('output.csv', 'w', newline='') as f_output:

Upvotes: 1

Related Questions