forked from HumanSignal/label-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add import json split (HumanSignal#1642)
* Add import json split * Fix desc * Add chunk arg * Add more help
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" This script splits IMPORT json with array into CHUNKS. | ||
This can be useful to avoid problems with a large json file during the import step. | ||
""" | ||
import sys | ||
import json | ||
|
||
INPUT = 'import.json' if len(sys.argv) <= 1 else sys.argv[1] | ||
OUTPUT = 'output' if len(sys.argv) <= 2 else sys.argv[2] | ||
CHUNKS = 2 if len(sys.argv) <= 3 else int(sys.argv[3]) | ||
print('Usage: python ' + sys.argv[0] + ' import.json output 10') | ||
|
||
if __name__ == '__main__': | ||
with open(INPUT) as f: | ||
j = json.load(f) | ||
|
||
total = len(j) | ||
chunk_size = int(total / float(CHUNKS)) | ||
chunk_size = 1 if chunk_size < 1 else chunk_size | ||
|
||
start = 0 | ||
count = 0 | ||
while start < len(j): | ||
filename = OUTPUT + str(count) + '.json' | ||
print(filename, '<=', INPUT, '[', start, ':', start+chunk_size, ']') | ||
with open(filename, 'w') as out: | ||
json.dump(j[start:start+chunk_size], out) | ||
|
||
start += chunk_size | ||
count += 1 |