UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 16: illegal multibyte sequence

Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 16: illegal multibyte sequence。

q_path = r"C:\Users\青尘\Desktop\南航电网项目\问答机器人\corpus\dnn\recall\Q.txt"
a_path = r"C:\Users\青尘\Desktop\南航电网项目\问答机器人\corpus\dnn\recall\A.txt"
excel_path = r"C:\Users\青尘\Desktop\南航电网项目\问答机器人\corpus\dnn\recall\excel.xlsx"

q_lines = open(q_path).readlines()
a_lines = open(a_path).readlines()
print(q_lines)

出现错误:

解决办法:

在open打开文件时编码UTF-8即可

q_lines = open(q_path, encoding="UTF-8").readlines()
a_lines = open(a_path, encoding="UTF-8").readlines()
posted @ 2021-06-01 16:00  时光如你般美好  阅读(994)  评论(0编辑  收藏  举报