+3 votes
in Programming Languages by (73.8k points)

I am trying to read a file using the following code. But the line "for rec in fi" gives UnicodeDecodeError.

with open(ifile, 'r') as fi:
    for rec in fi:

UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 525-526: invalid continuation byte

How can I fix this error?

1 Answer

+2 votes
by (348k points)
selected by
 
Best answer

It seems that your file has some characters that are not 'utf-8' and hence 'utf-8' codec cannot decode them.

When you open your input file, you can explicitly mention the encoding. The list of standard encodings supported by Python is here.

Try the following code and if it does not work, you can try other encodings mentioned in the above link.

with open(ifile, 'r', encoding='latin-1') as fi:
    for rec in fi:


...