import os import json def decode_text(text): """ Преобразует строки с ошибкой кодировки из Latin-1 в UTF-8. """ try: return text.encode('latin1').decode('utf-8') except (UnicodeEncodeError, UnicodeDecodeError): return text def recursive_decode(obj): """ Рекурсивно декодирует строки в объекте JSON. """ if isinstance(obj, str): return decode_text(obj) elif isinstance(obj, list): return [recursive_decode(item) for item in obj] elif isinstance(obj, dict): return {key: recursive_decode(value) for key, value in obj.items()} return obj def process_json_file(file_path): """ Обрабатывает JSON-файл, декодируя строки с ошибочной кодировкой Latin-1. Создает новый файл с суффиксом '_decoded'. """ with open(file_path, 'r', encoding='utf-8') as file: data = json.load(file) # Рекурсивно декодируем все строки в JSON-структуре decoded_data = recursive_decode(data) # Создаем новый файл с суффиксом '_decoded' new_file_path = f"{os.path.splitext(file_path)[0]}_decoded.json" with open(new_file_path, 'w', encoding='utf-8') as new_file: json.dump(decoded_data, new_file, ensure_ascii=False, indent=4) print(f"Создан файл: {new_file_path}") def process_directory(directory_path): """ Рекурсивно обрабатывает все JSON-файлы в указанной директории. """ for root, _, files in os.walk(directory_path): for file_name in files: if file_name.endswith('.json'): file_path = os.path.join(root, file_name) print(f"Обработка файла: {file_path}") process_json_file(file_path) # Укажите путь к папке с JSON-файлами directory_path = 'E:\code\kvant.app' process_directory(directory_path)