代码之家  ›  专栏  ›  技术社区  ›  Mahsa

我无法读取文件,因为我收到“UnicodeDecodeError:‘utf-8’编解码器无法解码”错误

  •  1
  • Mahsa  · 技术社区  · 9 年前

    我有一个文件,想把它转换成utf8编码。

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 947: invalid continuation byte
    

    我的目的是读取它,然后将其转换为utf8编码格式,但它不允许读取。

    这是我的代码:

    #convert all files into utf_8 format
    import os
    import io
    path_directory="some path string"
    directory = os.fsencode(path_directory)
    for file in os.listdir(directory):
        file_name=os.fsdecode(file)
        file_path_source=path_directory+file_name
        file_path_dest="some address to destination file"
        with open(file_path_source,"r") as f1:
            text=f1.read()
        with io.open(file_path_dest,"w+",encoding='utf8') as f2:
            f2.write(text)
        file_path=""
        file_name=""
        text=None
    

    误差为:

    ---------------------------------------------------------------------------
    UnicodeDecodeError                        Traceback (most recent call last)
    <ipython-input-47-59e5e52ddd40> in <module>()
         10     with open(file_path,"r") as f1:
         11         print(type(f1))
    ---> 12         text=f1.read()
         13     with io.open(file_path.replace("ref_sum","ref_sum_utf_8"),"w+",encoding='utf8') as f2:
         14         f2.write(text)
    
    /home/afsharizadeh/anaconda3/lib/python3.6/codecs.py in decode(self, input, final)
        319         # decode input (taking the buffer into account)
        320         data = self.buffer + input
    --> 321         (result, consumed) = self._buffer_decode(data, self.errors, final)
        322         # keep undecoded input until the next call
        323         self.buffer = data[consumed:]
    
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 947: invalid continuation byte
    

    1 回复  |  直到 9 年前
        1
  •  1
  •   0decimal0    9 年前

    这是显而易见的。如果你想打开一个文件,而它不是 utf8 是python3的默认编码,并且 对于python2),那么在打开文件时,您必须提及您知道的文件编码:

    io.open(file_path_dest,"r",encoding='ISO-8859-1')
    

    所以你必须提一下。