Home  >  Article  >  Backend Development  >  How to Read a UTF-8 CSV File with Python Without Encoding Errors?

How to Read a UTF-8 CSV File with Python Without Encoding Errors?

Susan Sarandon
Susan SarandonOriginal
2024-11-03 12:40:311050browse

How to Read a UTF-8 CSV File with Python Without Encoding Errors?

Reading a UTF8 CSV File with Python

Problem

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.

Solution

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn