Fix invalid JSONL format
Browse files- fix_jsonl.py +38 -0
- train.jsonl +0 -7
fix_jsonl.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
input_file = "train.jsonl"
|
| 4 |
+
output_file = "clean_train.jsonl"
|
| 5 |
+
|
| 6 |
+
valid_count = 0
|
| 7 |
+
invalid_count = 0
|
| 8 |
+
|
| 9 |
+
with open(input_file, "r", encoding="utf-8") as infile, open(output_file, "w", encoding="utf-8") as outfile:
|
| 10 |
+
|
| 11 |
+
for line_number, line in enumerate(infile, start=1):
|
| 12 |
+
|
| 13 |
+
line = line.strip()
|
| 14 |
+
|
| 15 |
+
if not line:
|
| 16 |
+
continue
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
obj = json.loads(line)
|
| 20 |
+
|
| 21 |
+
# Ensure object is dictionary
|
| 22 |
+
if isinstance(obj, dict):
|
| 23 |
+
|
| 24 |
+
outfile.write(json.dumps(obj) + "\n")
|
| 25 |
+
|
| 26 |
+
valid_count += 1
|
| 27 |
+
|
| 28 |
+
else:
|
| 29 |
+
invalid_count += 1
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
|
| 33 |
+
invalid_count += 1
|
| 34 |
+
|
| 35 |
+
print(f"Invalid line {line_number}: {e}")
|
| 36 |
+
|
| 37 |
+
print(f"Valid rows: {valid_count}")
|
| 38 |
+
print(f"Invalid rows removed: {invalid_count}")
|
train.jsonl
CHANGED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
"dataset_name"
|
| 2 |
-
"version"
|
| 3 |
-
"generated_at"
|
| 4 |
-
"total_samples"
|
| 5 |
-
"description"
|
| 6 |
-
"schema_version"
|
| 7 |
-
"samples"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|