-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.py
47 lines (37 loc) · 1.14 KB
/
clean.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 11 22:05:07 2019
@author: lukeum
"""
import os
import rapidjson as json
from tqdm import tqdm
from collections import defaultdict
data_dir = '/gpfs/accounts/lingjzhu_root/lingjzhu1/lingjzhu/users'
subreddits = os.listdir(data_dir)
path = '/gpfs/accounts/lingjzhu_root/lingjzhu1/lingjzhu/users_split'
# split jsons
for sub in tqdm(subreddits):
out_folder = os.path.join(path,sub)
if not os.path.exists(out_folder):
os.mkdir(out_folder)
user_dict = json.load(open(os.path.join(data_dir,sub),'r'))
for month, users in user_dict.items():
out = os.path.join(out_folder,month)
with open(out,'w') as f:
json.dump(users,f)
'''
# merge jsons
for sub in tqdm(subreddits):
out_file = os.path.join(path,sub)
user_dict = defaultdict()
times = os.listdir(os.path.join(data_dir,sub))
for t in times:
monthly = os.path.join(data_dir,sub,t)
with open(monthly,'r') as f:
data = json.load(f)
user_dict[t] = data
with open(out_file,'w') as f:
json.dump(user_dict,f)
'''