使用Python读取UTF8 CSV文件
我正在尝试使用Python读取带重音字符的CSV文件(仅法语和/或西班牙语字符)。基于csvreader的Python 2.5文档(http://docs.python.org/library/csv.html),我提出了以下代码来读取CSV文件,因为csvreader仅支持ASCII。 def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): # csv.py doesn't do Unicode; encode temporarily as UTF-8: csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), dialect=dialect, **kwargs) for row in csv_reader: # decode UTF-8 back to Unicode, cell by cell: yield [unicode(cell, 'utf-8') for cell in row] def utf_8_encoder(unicode_csv_data): for line in unicode_csv_data: yield line.encode('utf-8') filename = …