面试题答案
一键面试try:
with open('your_file.txt', 'rb') as f:
content = f.read()
try:
# 先尝试用UTF - 8解码
decoded_content = content.decode('utf-8')
except UnicodeDecodeError:
# 如果失败,用 'ignore' 错误处理方式进行解码
decoded_content = content.decode('utf-8', 'ignore')
print("存在编码错误,已忽略错误字符进行解码。")
print("转换后的Unicode字符串:", decoded_content)
except FileNotFoundError:
print("文件未找到。")