我正在尝试将大 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 中成功读取此内容,没有任何错误。
I tried
with open('h.docx', 'r', encoding='latin-1') as f:
a = f.read()
这样我可以毫无错误地读取文件。但是当写入另一个文件时,原始内容就会丢失。
也尝试过errors='surrogateescape'
,但是当写入另一个文件时,原始内容会丢失。