Advertisement
Guest User

fix_json.py

a guest
Dec 5th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | Source Code | 0 0
  1. import json_fix
  2.  
  3. def fix_instructions(file_path):
  4.     with open(file_path, 'r', encoding='utf-8-sig') as f:
  5.         lines = f.readlines()
  6.  
  7.     for i, line in enumerate(lines):
  8.         if line.strip().startswith('"instruction":'):
  9.             first_quote = line.find('"', line.find(':'))
  10.             last_quote = line.rfind('"')
  11.             # Remove all problematic characters between the quotes
  12.             new_line = line[:first_quote+1] + ''.join(c for c in line[first_quote+1:last_quote] if c.isalnum() or c.isspace()) + line[last_quote:]
  13.             lines[i] = new_line
  14.  
  15.     with open(file_path, 'w', encoding='utf-8-sig') as f:
  16.         f.writelines(lines)
  17.  
  18. file_path = r"C:\SD\updated_updated_combined.json"
  19. fix_instructions(file_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement