-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjsonl2files
More file actions
executable file
·47 lines (38 loc) · 1.11 KB
/
jsonl2files
File metadata and controls
executable file
·47 lines (38 loc) · 1.11 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*- vim:shiftwidth=4:expandtab:
#
# Sort JSONL by a value of the specified key and write to the value-named files
#
# SPDX-FileCopyrightText: 2025 SATOH Fumiyasu @ OSSTech Corp., Japan
# SPDX-License-Identifier: GPL-3.0-or-later
#
# /// script
# requires-python = ">=3.6"
# dependencies = [
# ]
# ///
import sys
import json
def main(argv):
key = argv[0]
ext = argv[1]
names = set()
for line_no, line in enumerate(sys.stdin, start=1):
try:
entry = json.loads(line)
except json.decoder.JSONDecodeError:
print(f"ERROR: Invalid JSONL input: line {line_no}: {line!r}", file=sys.stderr)
sys.exit(1)
name = entry[key] if key in entry else "NONE"
fname = f"{name}.{ext}"
if name in names:
os.remove(fname)
names.append(name)
with open(fname, "a") as f:
print(json.dumps(entry), file=f)
sys.exit(0)
if __name__ == '__main__':
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} KEY FILEEXT")
sys.exit(1)
sys.exit(main(sys.argv[1:]))