解决 python 读取文件乱码问题(UnicodeDecodeError)

解决 python 读取文件乱码问题(UnicodeDecodeError)

确定你的文件的编码,下面的代码将以'utf-8'为例,否则会忽略编码错误导致输出乱码

解决方案一

with open(r'/Users/mac/Desktop/face/2.1.docx', 'rb', ) as fr:
    data = fr.read()
    line_list = data.decode('utf8').split('\n')

    data_l = []
    for line in line_list:
        line = line.split('\t')[0]
        print(line)
        data_l.append(line)

解决方案二

with open(r'/Users/mac/Desktop/face/2.1.docx', 'r', encoding='utf8') as fr:
    data = fr.read()
    print(data)
posted @ 2019-04-01 08:17  B站-水论文的程序猿  阅读(5620)  评论(0编辑  收藏  举报