перепиши код для использования всех ядер процессора import os import chardet def…
- User
перепиши код для использования всех ядер процессора
import os import chardet
def is_utf8(text): try: text.decode('utf-8') return True except UnicodeDecodeError: return False
def clean_utf8(file_path): with open(file_path, 'rb') as file: content = file.read()
code# Use chardet to detect the file encoding result = chardet.detect(content) encoding = result['encoding'] if encoding is not None and is_utf8(content): return content.decode('utf-8', 'ignore') else: print(f"File {file_path} is not UTF-8 encoded. Skipping.") return Nonedef save_cleaned_content(file_path, cleaned_content): if cleaned_content is not None: with open(file_path, 'w', encoding='utf-8') as file: file.write(cleaned_content)
folder_path = 'base' files = os.listdir(folder_path)
for file_name in files: if file_name.endswith(".txt"): print(file_name) file_path = os.path.join(folder_path, file_name) cleaned_content = clean_utf8(file_path) save_cleaned_content(file_path, cleaned_content)
- Assistant
import os import chardet import multiprocessing
def is_utf8(text): try: text.decode('utf-8') return True except UnicodeDecodeError: return False
def clean_utf8(file_path): with open(file_path, 'rb') as file: content = file.read()
code# Use chardet to detect the file encoding result = chardet.detect(content) encoding = result['encoding'] if encoding is not None and is_utf8(content): return content.decode('utf-8', 'ignore') else: print(f"File {file_path} is not UTF-8 encoded. Skipping.") return Nonedef save_cleaned_content(file_path, cleaned_content): if cleaned_content is not None: with open(file_path, 'w', encoding='utf-8') as file: file.write(cleaned_content)
folder_path = 'base' files = os.listdir(folder_path)
pool = multiprocessing.Pool() results = []
for file_name in files: if file_name.endswith(".txt"): print(file_name) file_path = os.path.join(folder_path, file_name) result = pool.apply_async(clean_utf8, (file_path,)) results.append(result)
- User
перепиши выставив пробелы в табуляции
- Assistant
import os import chardet import multiprocessing
def is_utf8(text): try: text.decode('utf-8') return True except UnicodeDecodeError: return False
def clean_utf8(file_path): with open(file_path, 'rb') as file: content = file.read()
code# Use chardet to detect the file encoding result = chardet.detect(content) encoding = result['encoding'] if encoding is not None and is_utf8(content): return content.decode('utf-8', 'ignore') else: print(f"File {file_path} is not UTF-8 encoded. Skipping.") return Nonedef save_cleaned_content(file_path, cleaned_content): if cleaned_content is not None: with open(file_path, 'w', encoding='utf-8') as file: file.write(cleaned_content)