Upload examples/read_records.py
Browse files- examples/read_records.py +23 -17
examples/read_records.py
CHANGED
|
@@ -1,17 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Read compressed StreamStop JSONL records without extracting the full collection."""
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import argparse
|
| 4 |
+
import gzip
|
| 5 |
+
import json
|
| 6 |
+
import lzma
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def iter_records(path):
|
| 10 |
+
path = Path(path)
|
| 11 |
+
opener = lzma.open if path.suffix == ".xz" else gzip.open
|
| 12 |
+
with opener(path, "rt", encoding="utf-8") as stream:
|
| 13 |
+
for line in stream:
|
| 14 |
+
if line.strip():
|
| 15 |
+
yield json.loads(line)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
if __name__ == "__main__":
|
| 19 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 20 |
+
parser.add_argument("path", type=Path)
|
| 21 |
+
args = parser.parse_args()
|
| 22 |
+
row = next(iter_records(args.path))
|
| 23 |
+
print("Record fields:", ", ".join(sorted(row)))
|