编码处理问题总结:UnicodeDecodeError:'gbk' codec can't decode byte 0xe3: illegal multibyte sequence与读取docx

在试图打开docx文档内容时,以为可以向读取txt文档一样,于是写下了下面的代码

with open('C:\\Users\\Administrator\\Desktop\\案例二.docx','r')as f:
contents = f.read()
print(contents)

结果遇上报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xe3 in position 55: illegal multibyte sequence

在这里插入图片描述

解决方法一:
一看,编码错误,祖传方法encoding='utf-8‘’百试百灵的修改

with open('C:\\Users\\Administrator\\Desktop\\案例二.docx','r',encoding='utf-8‘’)as f:
	contents = f.read()
	print(contents)

结果一样报错UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x87 in position 10: invalid start byte
在这里插入图片描述
我就纳闷了,怎么还有 utf-8都解码不了,utf-8号称‘万国码’’(UTF-8编码:它是一种全国家通过的一种编码,如果网站涉及到多个国家的语言,那么建议选择UTF-8编码。),基本上用上它一切就ok了,怎么还报错。我就打了一个“你好”在里面啊!

但既然是编码错误,就继续。
之后按照这篇文章《UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xe9 in position 7581: illegal multibyte sequence》
一个一个换编码

gbk
gb2312
gb18030
utf-8
utf-16
utf-32
ISO-8859-1

都没有效果,
utf-16:UnicodeDecodeError: ‘utf-16-le’ codec can’t decode bytes in position 92-93: illegal encoding
在这里插入图片描述
ISO-8859-1:UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\x87’ in position 11: illegal multibyte sequence

在这里插入图片描述

都没有效果
解决方法二:
可能是不认识的编码。于是按照《使用chardet判断编码方式》使用chardet进行编码自动判断并调用

import chardet

def chardets():
	path = 'C:\\Users\\Administrator\\Desktop\\案例二.docx'
	with open(path, 'rb') as f:
    	#print(chardet.detect(f.read())['encoding'])
    	return chardet.detect(f.read())['encoding']
#chardets()
with open('C:\\Users\\Administrator\\Desktop\\案例二.docx','r', 	encoding=chardets())as f:
	contents = f.read()
	print(contents)

然而依然保错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xe3 in position 55: illegal multibyte sequence
在这里插入图片描述
看看是什么样的编码这样难以解决

print(chardet.detect(f.read())['encoding'])

在这里插入图片描述
怎么是None呢?

def chardets():
path = 'C:\\Users\\Administrator\\Desktop\\案例二.docx'
with open(path, 'rb') as f:
    print(chardet.detect(f.read()))#chardet.detect()返回的是一个字典,所以之前需要[‘encoding’]获取编码方式
    #return chardet.detect(f.read())['encoding']
chardets()

结果一样是None
在这里插入图片描述
于是我再次尝试了txt文件的读取,结果正常读取

在这里插入图片描述
但是有意思的是,其编码竟然是

{'encoding': 'TIS-620', 'confidence': 0.3598212120361634, 'language': 'Thai'}

这个编码从未见过,于是我想可能是文件中存在异常的编码(之前写入的不是“你好”这两个字符,而是一篇文章)
解决方法三:
编码解决不了,那就解决出现问题的编码,对之进行跳过。
增加errors=‘ignore’

import chardet

def chardets():
path = 'C:\\Users\\Administrator\\Desktop\\案例二.docx'
with open(path, 'rb') as f:
    print(chardet.detect(f.read()))
    return chardet.detect(f.read())['encoding']
#chardets()
with open('C:\\Users\\Administrator\\Desktop\\案例二.docx','r', encoding=chardets(), errors='ignore')as f:
contents = f.read()
print(contents)

结果乱码

在这里插入图片描述
之后尝试过对文档内容进行删除,查找是否存在异常的内容。
结果删成了上面的“你好”两个字符还是报错或者乱码。

尝试二进制byte写入后再gbk、utf-8读取,结果全是二进制内容
在这里插入图片描述

解决方法四(最终解决方法):
既然一致出现解码错误,而在读取txt时发现其编码为TIS-620,我想是不是文件格式导致的问题,于是查了一下读取docx的模块,结果出现了docx读取的模块,看来就是文档格式导致的问题了。
于是按照《Python学习笔记(28)-Python读取word文本》下载了python-docx
在这里插入图片描述

import docx
file=docx.Document("C:\\Users\\Administrator\\Desktop\\案例二.docx")
for para in file.paragraphs:
	print(para.text)

总结:有时错误是由于其上层错误导致的,而不是自己的问题,在找不到错误的情况下需要找找其上层依赖的问题是否存在。
[1]https://blog.csdn.net/Katrina_ALi/article/details/80638972?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158848764019725256734556%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.57662%2522%257D&request_id=158848764019725256734556&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-6

【2】https://blog.csdn.net/woshisangsang/article/details/75221723?ops_request_misc=&request_id=&biz_id=102&utm_medium=distribute.pc_search_result.none-task-blog-2blogsobaiduweb~default-0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章