我正在尝试将大型docx文件拆分为小文件。使用以下代码读取 python3.6 中的文件时。
with open('h.docx', 'r') as f:
a = f.read()
它会抛出此错误。
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/local/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xea in position
10: invalid continuation byte
h.docx是使用LibreOffice Calc创建的,其中只有'hello world'
作为内容。我可以在Python 2.7中成功阅读,没有任何错误。
我试过
with open('h.docx', 'r', encoding='latin-1') as f:
a = f.read()
在此我可以毫无错误地读取文件。但是当写入另一个文件时,原始内容将丢失。
还尝试了errors='surrogateescape'
,但是当写入另一个文件时,原始内容将丢失。
答案 0 :(得分:0)
不是答案,但评论时间太长。你所做的只是没有意义:你正试图读一个&#34; .docx&#34;文件好像是一个不的文本文件。这是一种复杂的格式,其中几个xml文件(可能还有其他...)连接成一个zip文件。您甚至不应考虑手工处理文件,除非:
甚至那些都不是简单的操作。
有可能: