-
【2020ERRORS】文件处理操作出现的错误-UnicodeDecodeError: 'gbk' codec can't decode byte 0
一、文件处理
错误类型:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 14: illegal multibyte sequence
1、 t 模式下的 读操作
新建txt文件 313.txt ——
hello world
hello day
hello me@2020
>>> f=open(r'D:\0tempt\313.txt',mode='rt') >>> print(f) <_io.TextIOWrapper name='D:\\0tempt\\313.txt' mode='rt' encoding='cp936'> >>> res=f.read() >>> print(res) hello world hello day hello mili @2020 >>>
新建txt文件 3133.txt——
你好,世界
你好,每一天
你好,米粒
>>> f=open(r'D:\0tempt\3133.txt',mode='rt') >>> print(f) <_io.TextIOWrapper name='D:\\0tempt\\3133.txt' mode='rt' encoding='cp936'> >>> res=f.read() Traceback (most recent call last): File "<pyshell#46>", line 1, in <module> res=f.read() UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 14: illegal multibyte sequence >>>
UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 14: illegal multibyte sequence
解决方法: 指定解码编码格式——encoding='UTF-8'
>>> f=open(r'D:\0tempt\3133.txt',mode='rt',encoding='UTF-8') >>> print(f) <_io.TextIOWrapper name='D:\\0tempt\\3133.txt' mode='rt' encoding='UTF-8'> >>> res=f.read() >>> print(res) 你好,世界 你好,每一天 你好,米粒 >>>
=====
之前英文和数字的文本内容,读取时没有指定解码编码,没有出错。是因为英文和数字是不会出现乱码现象的,使用任何编码类型解释器都能识别英文和数字。
最新更新
利用C#输出图形
利用C#计算所有整数之和
利用C#进行加密
元旦快到了,想好去哪玩了吗?Python分析
Python练习题
关于数据抓取很多新人的误区
数据分析 Pandas 简介和它的的数据结构
1319联通网络的操作次数
python字符串前加r、f、u、l 的区别
Python爬虫入门教程01:豆瓣Top电影爬取
用 Access+Outlook 来采集信息
使用PowerDesigner生成Access数据库
让我们一起用开源数据库和开源框架废弃
随说秋色园从Access升迁到MSSQL过程
当爬虫被拒绝时(Access Denied)
Web API与OAuth:既生access token,何生refres
[认证 & 授权] 6. Permission Based Access Co
Access之C#连接Access
oracle 19c下载和安装教程(database和client)
MySql索引原理与使用大全
php的计数器每次都会清零
PHP基础
数据类型之对象
数据类型之布尔型、整型、浮点型和字符
php教程之数据类型之数组
php教程之PHP 常量
php教程之变量
php教程之语法
PHP简介与安装
phpMyAdmin配置安装全攻略