def read_unicode_csv_fileobj(fileobj, delimiter=',', quotechar='"',
                             quoting=csv.QUOTE_MINIMAL, lineterminator='\n',
                             encoding='utf-8', skiprows=0):
    """fileobj can be a StringIO in Py3, but should be a BytesIO in Py2."""
    # Python 3 version
    if sys.version_info[0] >= 3:
        # Next, get the csv reader, with unicode delimiter and quotechar
        csv_reader = csv.reader(fileobj, delimiter=delimiter,
                                quotechar=quotechar, quoting=quoting,
                                lineterminator=lineterminator)
        # Now, return the (already decoded) unicode csv_reader generator
        # Skip rows if necessary
        for skip_ix in range(skiprows):
            next(csv_reader)
        for row in csv_reader:
            yield row
    # Python 2 version
    else:
        # Next, get the csv reader, passing delimiter and quotechar as
        # bytestrings rather than unicode
        csv_reader = csv.reader(fileobj, delimiter=delimiter.encode(encoding),
                             quotechar=quotechar.encode(encoding),
                             quoting=quoting, lineterminator=lineterminator)
        # Iterate over the file and decode each string into unicode
        # Skip rows if necessary
        for skip_ix in range(skiprows):
            next(csv_reader)
        for row in csv_reader:
            yield [cell.decode(encoding) for cell in row]
def read_unicode_csv_fileobj ( fileobj , delimiter = ',' , quotechar = '"' , quoting = csv . QUOTE_MINIMAL , lineterminator = '\n' , encoding = 'utf-8' , skiprows = 0 ) : if sys . version_info [ 0 ] >= 3 : csv_reader = csv . reader ( fileobj , delimiter = delimiter , quotechar = quotechar , quoting = quoting , lineterminator = lineterminator ) for skip_ix in range ( skiprows ) : next ( csv_reader ) for row in csv_reader : yield row else : csv_reader = csv . reader ( fileobj , delimiter = delimiter . encode ( encoding ) , quotechar = quotechar . encode ( encoding ) , quoting = quoting , lineterminator = lineterminator ) for skip_ix in range ( skiprows ) : next ( csv_reader ) for row in csv_reader : yield [ cell . decode ( encoding ) for cell in row ]
fileobj can be a StringIO in Py3, but should be a BytesIO in Py2.