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

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

提交回复
热议问题