('input_file.txt', 'r', encoding='utf-8') as file: content = file.read() # 检查并替换非UTF-8字符(可选) content = content.encode('utf-8', 'ignore').decode('utf-8') # 写入文件时指定UTF-8编码 with open('output_file.txt', 'w', encoding='utf-8') as file: file.write(...