UnicodeDecodeError: 'gbk' codec can't decode byte 0xb0 in position 279: illegal multibyte sequence

with open(r'E:\yy\mysql.txt') as wk:
    print(wk.readlines())
Traceback (most recent call last):
  File "C:\eclipse-workspace\py3_test\test\test3.py", line 8, in <module>
    print(wk.readlines())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb0 in position 279: illegal multibyte sequence

读取文件时,报错,解决方案:

with open(r'E:\yy\mysql.txt',encoding='utf-8') as wk:
    print(wk.readline())

这种方法读取的内容是字符串类型

<class 'str'>

with open(r'E:\yy\mysql.txt','rb') as wk:
    print(wk.readline())

这种方法读取的内容是字节类型

<class 'bytes'>

 

关于读取文件,首行会多 ‘\ufeff’ 的处理方法:https://www.cnblogs.com/chongzi1990/p/8694883.html

with open(r'E:\yy\mysql.txt',encoding='UTF-8-sig') as wk:

 

posted @ 2018-11-29 10:54  luckylemon  阅读(3169)  评论(0编辑  收藏  举报