“for line in…” results in UnicodeDecodeError: 'utf-8' codec can't decode byte

前端 未结 10 763
抹茶落季
抹茶落季 2020-11-22 17:15

Here is my code,

for line in open(\'u.item\'):
#read each line

whenever I run this code it gives the following error:



        
相关标签:
10条回答
  • 2020-11-22 17:42

    Your file doesn't actually contain utf-8 encoded data, it contains some other encoding. Figure out what that encoding is and use it in the open call.

    In Windows-1252 encoding for example the 0xe9 would be the character é.

    0 讨论(0)
  • 2020-11-22 17:51

    you can try this way:

    open('u.item', encoding='utf8', errors='ignore')
    
    0 讨论(0)
  • 2020-11-22 17:52

    You could resolve the problem with:

    for line in open(your_file_path, 'rb'):
    

    'rb' is reading file in binary mode. Read more here. Hope this will help!

    0 讨论(0)
  • 2020-11-22 17:53

    This works:

    open('filename', encoding='latin-1')
    

    or:

    open('filename',encoding="ISO-8859-1")
    
    0 讨论(0)
提交回复
热议问题