Last active
August 29, 2015 13:58
-
-
Save bpeterso2000/10277785 to your computer and use it in GitHub Desktop.
CSV Unicode reader with dual support for Python 2.6+ & 3.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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