Skip to content

Instantly share code, notes, and snippets.

@bpeterso2000
Last active August 29, 2015 13:58
Show Gist options
  • Save bpeterso2000/10277785 to your computer and use it in GitHub Desktop.
Save bpeterso2000/10277785 to your computer and use it in GitHub Desktop.
CSV Unicode reader with dual support for Python 2.6+ & 3.x
import csv
import sys
from codecs import iterencode, iterdecode
from io import open
PY2 = sys.version_info.major < 3
def csv_file(filename, dialect=csv.excel, encoding=None, **kwds):
"""
:yields: CSV rows as a list of Unicode column values
:raises: IOError, OSError, csv.Error,
UnicodeEncodeError, UnicodeDecodeError
"""
with open(filename, newline='', encoding=encoding) as file_:
csvfile = iterencode(file_, 'utf-8') if PY2 else file_
csvreader = csv.reader(csvfile, dialect=dialect, **kwds)
if PY2:
csvreader = (list(iterdecode(i, 'utf-8')) for i in csvreader)
for row in csvreader:
yield row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment