Fix with new file
Browse files- data/test.jsonl +0 -0
- parse_check.py +44 -0
data/test.jsonl
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
parse_check.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def fix_json_string(json_str):
|
| 6 |
+
# Replace single backslashes with double backslashes
|
| 7 |
+
# but only when they're not already properly escaped
|
| 8 |
+
return re.sub(r'(?<!\\)\\(?!["\\])', r'\\\\', json_str)
|
| 9 |
+
|
| 10 |
+
def check_and_fix_jsonl_file(input_file_path):
|
| 11 |
+
# Create output filename by adding '_fixed' before the extension
|
| 12 |
+
base, ext = os.path.splitext(input_file_path)
|
| 13 |
+
output_file_path = f"{base}_fixed{ext}"
|
| 14 |
+
|
| 15 |
+
fixed_lines = []
|
| 16 |
+
has_errors = False
|
| 17 |
+
|
| 18 |
+
with open(input_file_path, 'r', encoding='utf-8') as f:
|
| 19 |
+
for line_num, line in enumerate(f, 1):
|
| 20 |
+
try:
|
| 21 |
+
# Try to fix the JSON string before parsing
|
| 22 |
+
fixed_line = fix_json_string(line)
|
| 23 |
+
# Verify the fix worked by parsing
|
| 24 |
+
json.loads(fixed_line)
|
| 25 |
+
fixed_lines.append(fixed_line)
|
| 26 |
+
except json.JSONDecodeError as e:
|
| 27 |
+
has_errors = True
|
| 28 |
+
print(f"Error on line {line_num}: {str(e)}")
|
| 29 |
+
print(f"Problematic line content: {line.strip()}")
|
| 30 |
+
|
| 31 |
+
if has_errors:
|
| 32 |
+
print("\nSome lines had errors and may not be properly fixed.")
|
| 33 |
+
|
| 34 |
+
# Write the fixed lines to the new file
|
| 35 |
+
with open(output_file_path, 'w', encoding='utf-8') as f:
|
| 36 |
+
f.writelines(fixed_lines)
|
| 37 |
+
|
| 38 |
+
print(f"\nFixed JSONL file saved to: {output_file_path}")
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
file_path = "data/test.jsonl"
|
| 42 |
+
print(f"Checking and fixing file: {file_path}")
|
| 43 |
+
check_and_fix_jsonl_file(file_path)
|
| 44 |
+
print("Process complete")
|