Python3.6中的UnicodeDecodeError

时间:2017-05-25 12:40:56

标签: python-3.x terminal

我正在学习python3.6,我用以下代码编写了一个简单的程序:

import os

with open ('USA 205-403.docx', 'rt') as in_file: # open file lorem.txt for reading text data

    contents = in_file.read() # read the entire file into a string variable

print(contents) #print the document

当我尝试在终端中运行它时,这是我得到的UnicodeDecodeError:

Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> documentreader.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'documentreader' is not defined
>>> ^D
Jesses-MacBook-Pro-3:code for foundation data meduro$ python3.6 documentreader.py
Traceback (most recent call last):
  File "documentreader.py", line 5, in <module>
    contents = in_file.read() # read the entire file into a string variable
  File "/Library/Frameworks/Python.framework/Versions/3.6/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 0xca in position 14: invalid continuation byte

发生了什么事?当我在python2.7中运行它时,它会运行,但它会发出加密文本。

最终,我希望将此程序转换为文件阅读器,查找特定单词并打印围绕这些流行语的一定数量的文本

0 个答案:

没有答案