From c7ea5036a0bd6883d8643a36c9af7024bf0b7055 Mon Sep 17 00:00:00 2001 From: Dmitriy Kazimirov Date: Sat, 29 Mar 2025 23:59:31 +0600 Subject: [PATCH] Fix encoding to UTF-8 --- check_encoding.py | 22 ++++++++++++++++++++++ fix_encoding.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 check_encoding.py create mode 100644 fix_encoding.py diff --git a/check_encoding.py b/check_encoding.py new file mode 100644 index 0000000..38db325 --- /dev/null +++ b/check_encoding.py @@ -0,0 +1,22 @@ +import os +import chardet + +DIRECTORY = "./" # Замени на путь к папке с файлами + +def detect_encoding(filename): + with open(filename, "rb") as f: + raw_data = f.read() + result = chardet.detect(raw_data) + return result["encoding"] + +def check_all_files(directory): + for root, _, files in os.walk(directory): + for file in files: + filepath = os.path.join(root, file) + encoding = detect_encoding(filepath) + if encoding and "utf-16" in encoding.lower(): + print(f"❌ {filepath} - {encoding}") + +print("🔍 Поиск файлов с UTF-16...") +check_all_files(DIRECTORY) +print("✅ Готово!") \ No newline at end of file diff --git a/fix_encoding.py b/fix_encoding.py new file mode 100644 index 0000000..b052169 --- /dev/null +++ b/fix_encoding.py @@ -0,0 +1,29 @@ +import os +import chardet +import codecs + +DIRECTORY = "./local_files" # Путь к папке с файлами + +def convert_to_utf8(filename): + with open(filename, "rb") as f: + raw_data = f.read() + result = chardet.detect(raw_data) + encoding = result["encoding"] + + if encoding and "utf-16" in encoding.lower(): + print(f"🔄 Конвертирую {filename} ({encoding}) в UTF-8...") + with codecs.open(filename, "r", encoding=encoding) as f: + content = f.read() + with codecs.open(filename, "w", encoding="utf-8") as f: + f.write(content) + print(f"✅ {filename} теперь в UTF-8!") + +def fix_all_files(directory): + for root, _, files in os.walk(directory): + for file in files: + filepath = os.path.join(root, file) + convert_to_utf8(filepath) + +print("🔍 Исправление кодировки...") +fix_all_files(DIRECTORY) +print("✅ Готово!")