UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 18: illegal multibyte sequence
2017-11-03 11:10
746 查看
CSDN:http://blog.csdn.net/kicilove/article/
github:https://github.com/zhaohuicici?tab=repositories
经搜索发现问题在于知道是字符流的问题。
代码如下:
可见已经可以正常读入啦!
CSDN:http://blog.csdn.net/kicilove/article/
github:https://github.com/zhaohuicici?tab=repositories
github:https://github.com/zhaohuicici?tab=repositories
问题:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 18: illegal multibyte sequence原因:
本来想完成读文件,再写入另一文件的,但是在 fp.read() 时,一直遇到上面的错误经搜索发现问题在于知道是字符流的问题。
解决方法:
可以把 open 的方式变为 二进制,也就是下面代码里的 open(filename,’rb’), 这下好了,至少后面的read() 可以通过。代码如下:
# _*_coding:utf-8_*_ import re filename = 'train.txt' ### mark fp = open(filename,'rb') content1 = fp.read().decode('utf-8') reg = r'<p>(.*?)</p>' #注意content1的输出 print(content1) ##附:其他部分 result = re.findall(reg,content1) fp.close() fp = open(filename,'bw') for r in result: r = r.encode('utf-8') fp.write(r) fp.write('\n'.encode('utf-8')) fp.close() 结果输出: 0 你好吗?立夏了,心中又把你想起。愿会心的微笑,排满你每天的日历;抛弃压力,婴儿般惬意地呼吸;天气转热,别忘好好保重自己! 1 女子闯红灯被拦 脱鞋猛抽交警-图[腾讯]-中国移动冲浪助手:http://go.10086.cn/nd/lAz/cJctr 0 套餐余量提醒服务:截至本月05日,您定制的(1)预存返话费捆绑10元流量包包含的数据流量剩余49.15MB;本地数据流量剩余12.90MB;(2)全球通88商旅套餐包含的语音剩余254分钟;数据流量剩余30.00MB;感谢您的使用。
可见已经可以正常读入啦!
CSDN:http://blog.csdn.net/kicilove/article/
github:https://github.com/zhaohuicici?tab=repositories
相关文章推荐
- UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
- 'gbk' codec can't decode byte 0x94 in position 41:illegal multibyte sequence
- 【Python】Python读取文件报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 20: illegal multibyte sequence
- 读写文件中遇到'gbk' codec can't decode byte 0xa6 in position 36: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 1425: illegal multibyte sequence
- Python错误 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 11126: illegal multibyte sequence
- 提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequenc
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 11126: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xfd in position 3952: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 30738: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence
- Python中读取txt文本出现“ 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”的解决办法
- UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 1106: illegal multibyte s
- Python读取CSV文件:UnicodeDecodeError: 'gbk' codec can't decode byte 0xba ....illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 11126: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 11126: illegal multibyte sequence
- 'gbk' codec can't decode byte 0xaf in position 6532: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 683: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 11126: illegal multibyte sequence