Home > Article > Backend Development > How to Read a UTF-8 CSV File with Python Without Encoding Errors?
When attempting to read a CSV file containing special characters with Python, an exception is encountered. The code utilizes encoding and decoding to convert between ASCII and UTF-8, but this approach still produces an error.
The original code employs the .encode method incorrectly by applying it to a byte-string instead of a Unicode string. To resolve this issue, the following simplified code can be utilized:
<code class="python">import csv def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs): csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs) for row in csv_reader: yield [unicode(cell, 'utf-8') for cell in row] filename = 'da.csv' reader = unicode_csv_reader(open(filename)) for field1, field2, field3 in reader: print field1, field2, field3 </code>
This code efficiently reads UTF-8 encoded data into a Python list without the need for transcoding.
The above is the detailed content of How to Read a UTF-8 CSV File with Python Without Encoding Errors?. For more information, please follow other related articles on the PHP Chinese website!