def read_csv(csv_file, ext='<STR_LIT>', format=None, delete_empty_keys=False,<EOL>fieldnames=[], rowlimit=<NUM_LIT>, numbers=False, normalize_names=True, unique_names=True,<EOL>verbosity=<NUM_LIT:0>):
if not csv_file:<EOL><INDENT>return<EOL><DEDENT>if isinstance(csv_file, basestring):<EOL><INDENT>path = csv_file[:<NUM_LIT>]<EOL>try:<EOL><INDENT>fpin = open(path, '<STR_LIT>')  <EOL><DEDENT>except (IOError, FileNotFoundError):<EOL><INDENT>path = csv_file[:<NUM_LIT>]<EOL>fpin = StringIO(str(csv_file))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fpin = csv_file<EOL>try:<EOL><INDENT>path = csv_file.name<EOL><DEDENT>except (IndexError, ValueError, AttributeError, TypeError):<EOL><INDENT>path = '<STR_LIT>'<EOL><DEDENT><DEDENT>format = format or '<STR_LIT:h>'<EOL>format = format[<NUM_LIT:0>].lower()<EOL>csvr = csv.reader(fpin, dialect=csv.excel)<EOL>if not fieldnames:<EOL><INDENT>while not fieldnames or not any(fieldnames):<EOL><INDENT>fieldnames = strip_br([str(s).strip() for s in next(csvr)])<EOL><DEDENT>if verbosity > <NUM_LIT:0>:<EOL><INDENT>logger.info('<STR_LIT>' + repr(fieldnames))<EOL><DEDENT><DEDENT>if unique_names:<EOL><INDENT>norm_names = OrderedDict([(fldnm, fldnm) for fldnm in fieldnames])<EOL><DEDENT>else:<EOL><INDENT>norm_names = OrderedDict([(num, fldnm) for num, fldnm in enumerate(fieldnames)])<EOL><DEDENT>if normalize_names:<EOL><INDENT>norm_names = OrderedDict([(num, make_name(fldnm, **make_name.DJANGO_FIELD)) for num, fldnm in enumerate(fieldnames)])<EOL>model_name = make_name(path, **make_name.DJANGO_MODEL)<EOL><DEDENT>if format in '<STR_LIT:c>':  <EOL><INDENT>recs = OrderedDict((norm_name, []) for norm_name in list(norm_names.values()))<EOL><DEDENT>elif format in '<STR_LIT>':<EOL><INDENT>recs = [fieldnames]<EOL><DEDENT>else:<EOL><INDENT>recs = []<EOL><DEDENT>if verbosity > <NUM_LIT:0>:<EOL><INDENT>logger.info('<STR_LIT>' + repr(norm_names if normalize_names else fieldnames))<EOL><DEDENT>rownum = <NUM_LIT:0><EOL>eof = False<EOL>pbar = None<EOL>start_seek_pos = fpin.tell() or <NUM_LIT:0><EOL>if verbosity > <NUM_LIT:1>:<EOL><INDENT>print('<STR_LIT>'.format(start_seek_pos))<EOL><DEDENT>fpin.seek(<NUM_LIT:0>, os.SEEK_END)<EOL>file_len = fpin.tell() - start_seek_pos  <EOL>fpin.seek(start_seek_pos)<EOL>if verbosity > <NUM_LIT:1>:<EOL><INDENT>print(('<STR_LIT>' +<EOL>'<STR_LIT>').format(file_len))<EOL><DEDENT>while csvr and rownum < rowlimit and not eof:<EOL><INDENT>if pbar:<EOL><INDENT>pbar.update(fpin.tell() - start_seek_pos)<EOL><DEDENT>rownum += <NUM_LIT:1><EOL>row = []<EOL>row_dict = OrderedDict()<EOL>while not row or not any(len(x) for x in row):<EOL><INDENT>try:<EOL><INDENT>row = next(csvr)<EOL>if verbosity > <NUM_LIT:1>:<EOL><INDENT>logger.info('<STR_LIT>' + repr(row))<EOL><DEDENT><DEDENT>except StopIteration:<EOL><INDENT>eof = True<EOL>break<EOL><DEDENT><DEDENT>if eof:<EOL><INDENT>break<EOL><DEDENT>if len(row) and isinstance(row[-<NUM_LIT:1>], basestring) and len(row[-<NUM_LIT:1>]):<EOL><INDENT>row = strip_br(row)<EOL><DEDENT>if numbers:<EOL><INDENT>row = [tryconvert(v, desired_types=NUMBERS_AND_DATETIMES, empty=None, default=v) for v in row]<EOL><DEDENT>if row:<EOL><INDENT>N = min(max(len(row), <NUM_LIT:0>), len(norm_names))<EOL>row_dict = OrderedDict(<EOL>((field_name, field_value) for field_name, field_value in zip(<EOL>list(list(norm_names.values()) if unique_names else norm_names)[:N], row[:N])<EOL>if (str(field_name).strip() or delete_empty_keys is False))<EOL>)<EOL>if format in '<STR_LIT>':  <EOL><INDENT>recs += [{"<STR_LIT>": rownum, "<STR_LIT>": model_name, "<STR_LIT>": row_dict}]<EOL><DEDENT>elif format in '<STR_LIT>':  <EOL><INDENT>recs += [[value for field_name, value in viewitems(row_dict)<EOL>if (field_name.strip() or delete_empty_keys is False)]]<EOL><DEDENT>elif format in '<STR_LIT:c>':  <EOL><INDENT>for field_name in row_dict:<EOL><INDENT>recs[field_name] += [row_dict[field_name]]<EOL><DEDENT>if verbosity > <NUM_LIT:2>:<EOL><INDENT>print([recs[field_name][-<NUM_LIT:1>] for field_name in row_dict])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>recs += [row_dict]<EOL><DEDENT>if verbosity > <NUM_LIT:2> and format not in '<STR_LIT:c>':<EOL><INDENT>print(recs[-<NUM_LIT:1>])<EOL><DEDENT><DEDENT><DEDENT>if file_len > fpin.tell():<EOL><INDENT>logger.info("<STR_LIT>" % (fpin.tell(), file_len))<EOL><DEDENT>if pbar:<EOL><INDENT>pbar.finish()<EOL><DEDENT>fpin.close()<EOL>if not unique_names:<EOL><INDENT>return recs, norm_names<EOL><DEDENT>return recs<EOL>
r"""
    Read a csv file from a path or file pointer, returning a dict of lists, or list of lists (according to `format`)

    filename: a directory or list of file paths
    numbers: whether to attempt to convert strings in csv to numbers

    TODO:
        merge with `nlp.util.make_dataframe` function

    Handles unquoted and quoted strings, quoted commas, quoted newlines (EOLs), complex numbers,
        times, dates, datetimes

    >>> read_csv('"name\r\n",rank,"serial\nnumber",date <BR />\t\n"McCain, John","1","123456789",9/11/2001\n' +
    ...          'Bob,big cheese,1-23,1/1/2001 12:00 GMT', format='header+values list', numbers=True)
    [['name', 'rank', 'serial\nnumber', 'date'], ['McCain, John', 1.0, 123456789.0, '9/11/2001'],
    ['Bob', 'big cheese', '1-23', '1/1/2001 12:00 GMT']]