Home >Backend Development >Python Tutorial >How to Fix UnicodeDecodeError When Reading Files in Python?
UnicodeDecodeError with "for line in..."
When iterating through lines in a file using the "for line in..." syntax, you may encounter a UnicodeDecodeError if the file contains non-UTF-8 characters. This error occurs because the default encoding used by Python's open() function is UTF-8, which may not match the actual encoding of the file.
Solution:
To fix this error, you need to specify the correct file encoding when opening the file. The simplest way to do this is to use the encoding parameter of the open() function. For example:
<code class="python">for line in open('u.item', encoding='utf-8'): # Read each line</code>
However, if the file is not encoded in UTF-8, you will need to specify the correct encoding. In your case, the correct encoding for the file is "ISO-8859-1". To use this encoding, replace the open() call with:
<code class="python">for line in open('u.item', encoding='ISO-8859-1'): # Read each line</code>
This should resolve the UnicodeDecodeError and allow you to iterate through the lines in the file correctly.
The above is the detailed content of How to Fix UnicodeDecodeError When Reading Files in Python?. For more information, please follow other related articles on the PHP Chinese website!