-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_json.py
48 lines (27 loc) · 1.13 KB
/
split_json.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
48
# JSON Splitter
# Created by Brendon Albertson, 2021
import json
# create a dictionary (object)
oldData = {}
with open('sentences.json') as f:
oldData = json.load(f)
sentences = len(oldData['sentences'])
sentencesPerFile = 33333
howManyFiles = int( len(oldData['sentences']) / sentencesPerFile )
remainder = (sentences/sentencesPerFile - (int(sentences/sentencesPerFile)) ) * sentencesPerFile
globalCount = 0
def makeFile(fileId):
#need to "grab" the global variables before changing them inside the function:
global globalCount
newData = {'sentences':[]} #reset
i = 0
while i<sentencesPerFile:
newData['sentences'].append( oldData['sentences'][globalCount] )
i+=1
globalCount+=1
#dump the remainder items into the last JSON file:
# make a new file with these 100000 sentences:
with open('sentences'+str(fileId)+'.json', 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(newData, indent=4))
for n in range(howManyFiles):
makeFile(n)