forked from Ahsan196/FYP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertingTweetObjectJSONtoCSV.py
More file actions
37 lines (29 loc) · 1.37 KB
/
ConvertingTweetObjectJSONtoCSV.py
File metadata and controls
37 lines (29 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
import os
import csv
# Path to folder that contains the News folders (only folder names that start with "News-" are processed)
path = ""
folders = []
for root, dirs, files in os.walk(path, topdown=False):
folders = dirs
folders = [folder for folder in folders if folder.startswith("News-")]
# Python program to convert JSON file to C
#Specify the columns required in csv
keys=["screen_name","created_at","location","verified","followers_count","friends_count","favourites_count","statuses_count","listed_count"]
with open('PathWhereWantToCreateCSVFile/File.csv', 'a', encoding="utf-8-sig") as data_file:
writer=csv.DictWriter(data_file,fieldnames=keys)
writer.writeheader()
for folder in folders:
serial = folder.replace("News-", "")
json_file_path = F"{path}\\{folder}\\{serial}.json"
print(F" -- Processing {serial}.json")
with open(json_file_path, "r", encoding="utf-8-sig") as json_file:
data = json.load(json_file)
#For user in each Tweet Object,getting the User Object
for item in data:
user= item['user']
#Reading the row consisting of specifc columns in user as mentioned in keys
vdata = {key: value for key, value in user.items() if key in keys}
#Writing row to csv file
writer.writerow(vdata)
data_file.close()