| #!/usr/bin/env python3 | |
| import argparse | |
| from pathlib import Path | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Write the first N non-empty JSONL lines to a new file.") | |
| parser.add_argument("--input", required=True, help="Input JSONL file.") | |
| parser.add_argument("--output", required=True, help="Output JSONL file.") | |
| parser.add_argument("--limit", type=int, default=100000, help="Number of non-empty lines to keep.") | |
| args = parser.parse_args() | |
| input_path = Path(args.input).resolve() | |
| output_path = Path(args.output).resolve() | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| kept = 0 | |
| with input_path.open("r", encoding="utf-8") as src, output_path.open("w", encoding="utf-8") as dst: | |
| for line in src: | |
| if not line.strip(): | |
| continue | |
| dst.write(line) | |
| kept += 1 | |
| if kept >= args.limit: | |
| break | |
| print(f"Wrote {kept} lines to {output_path}") | |
| if __name__ == "__main__": | |
| main() | |