【问题标题】:Rodeo UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)Rodeo UnicodeDecodeError:“ascii”编解码器无法解码位置 0 的字节 0xef:序数不在范围内(128)
【发布时间】:2018-01-11 05:22:54
【问题描述】:

我刚开始使用 Python3。我正在尝试使用 Rodeo IDE 打开一个 csv 文件

fp = open('Proteomics_Data.csv') # open file on read mode
lines = fp.read().split("\n") # create a list containing all lines

我收到一个错误,我将其粘贴在下面。

UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-347-aebf19dd596a> in <module>()
----> 1 lines = fp.read().split("\n")
/Users/alessandro/anaconda/lib/python3.6/encodings/ascii.py in decode(self, input, final)
     24 class IncrementalDecoder(codecs.IncrementalDecoder):
     25     def decode(self, input, final=False):
---> 26         return codecs.ascii_decode(input, self.errors)[0]
     27 
     28 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

到目前为止,我发现终端没有设置为 UTF-8,但显然 Python 3 不需要 UTF-8。我不确定这是否是与 IDE 相关的问题?

【问题讨论】:

  • 尝试在open中指定编码
  • 有不同的编码,如 utf-8 utf-16 等,因此请尝试指定其中任何一种,看看是否有效
  • @AI14 为未来的裁判和读者发布了答案

标签: python-3.x csv utf-8


【解决方案1】:

尝试在open 函数调用中指定编码。

fp = open('Proteomics_Data.csv', encoding='utf-8')

【讨论】:

    【解决方案2】:

    当我将文件保存为 CSV 时,我实际上有两个选择:

    CSV UTF-8 (Comma delimited)(.csv)Comma Separated Values(.csv)

    如果我使用第二个选项(逗号分隔值)保存,我不需要添加 encoding='utf-8'

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 1970-01-01
      • 2017-03-30
      • 2021-08-08
      • 2011-05-13
      • 2014-02-19
      • 2018-07-26
      • 2020-11-06
      • 2013-09-20
      相关资源
      最近更新 更多