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

前端 未结 10 749
抹茶落季
抹茶落季 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:29

    Sometimes when open(filepath) in which filepath actually is not a file would get the same error, so firstly make sure the file you're trying to open exists:

    import os
    assert os.path.isfile(filepath)
    

    hope this will help.

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

    If you are using Python 2 the following will the solution:

    import io
    for line in io.open("u.item", encoding="ISO-8859-1"):
        # do something
    

    Because encoding parameter doesn't work with open(), you will be getting the following error:

    TypeError: 'encoding' is an invalid keyword argument for this function
    
    0 讨论(0)
  • As suggested by Mark Ransom, I found the right encoding for that problem. The encoding was "ISO-8859-1", so replacing open("u.item", encoding="utf-8") with open('u.item', encoding = "ISO-8859-1") will solve the problem.

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

    If someone looking for these, this is an example for converting a CSV file in Python 3:

    try:
        inputReader = csv.reader(open(argv[1], encoding='ISO-8859-1'), delimiter=',',quotechar='"')
    except IOError:
        pass
    
    0 讨论(0)
  • 2020-11-22 17:38

    Also worked for me, ISO 8859-1 is going to save a lot, hahaha, mainly if using Speech Recognition API's

    Example:

    file = open('../Resources/' + filename, 'r', encoding="ISO-8859-1");
    
    0 讨论(0)
  • 2020-11-22 17:38

    Try this to read using pandas

    pd.read_csv('u.item', sep='|', names=m_cols , encoding='latin-1')
    
    0 讨论(0)
提交回复
热议问题