-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_to_jsonl_binder.py
More file actions
51 lines (38 loc) · 1.81 KB
/
Copy pathjson_to_jsonl_binder.py
File metadata and controls
51 lines (38 loc) · 1.81 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
38
39
40
41
42
43
44
45
46
47
48
49
50
import os, json
import pandas as pd
############################################################
# Define Output Location
############################################################
current_path = os.getcwd()
directory = os.path.join(current_path,"json") #Specify your json directory path here
############################################################
# Changing File format to Json (NOT NECCESSARY IN THE DATA SAMPLING VERSIONS ABOVE 06/03/2020)
############################################################
#for file in os.listdir(directory): # This can rename your file in case they do NOT already end in .json
# dst = directory + "\\" +file + ".json"
# src = directory + "\\" +file
# os.rename(src, dst)
############################################################
# Reading in all Jsons to one Python object
############################################################
json_list = [] # Initiate a new blank list for storing json data in list format
for dirpath, subdirs, files in os.walk(directory):
for file in files:
if file.endswith(".json"):
with open(os.path.join(dirpath, file)) as json_file:
data = json.load(json_file)
json_list.append(data)
############################################################
# Saving Python Object to JsonL
############################################################
with open('output.jsonl', 'w') as outfile: #Now, output the list of json data into a single jsonl file
for entry in json_list:
json.dump(entry, outfile)
outfile.write('\n')
############################################################
# Test if you can access tweets independently
############################################################
tweets = []
for line in open('output.jsonl', 'r'):
tweets.append(json.loads(line))
print(tweets[1])