def reader(fname, header=True, sep="<STR_LIT:\t>", skip_while=None,<EOL>quotechar='<STR_LIT:">'):
if isinstance(fname, int_types):<EOL><INDENT>fname = sys.argv[fname]<EOL><DEDENT>if not isinstance(fname, basestring) andisinstance(fname, types.GeneratorType):<EOL><INDENT>line_gen = fname<EOL><DEDENT>elif sep is None:<EOL><INDENT>def _line_gen(f, sep):<EOL><INDENT>if sep is None:<EOL><INDENT>for line in nopen(f):<EOL><INDENT>yield line.rstrip("<STR_LIT:\r\n>").split()<EOL><DEDENT><DEDENT><DEDENT>line_gen = _line_gen(fname, sep)<EOL><DEDENT>elif isinstance(fname, basestring) and fname.endswith(("<STR_LIT>")):<EOL><INDENT>line_gen = xls_reader(fname)<EOL><DEDENT>else:<EOL><INDENT>if len(sep) == <NUM_LIT:1> or (len(sep) == <NUM_LIT:2> and sep[<NUM_LIT:0>] == '<STR_LIT:\\>'):<EOL><INDENT>dialect = csv.excel<EOL>dialect.delimiter = sep<EOL>dialect.quotechar = quotechar<EOL>if not quotechar:<EOL><INDENT>dialect.quoting = csv.QUOTE_NONE<EOL><DEDENT>line_gen = csv.reader(nopen(fname), dialect=dialect)<EOL><DEDENT>else: <EOL><INDENT>import re<EOL>sep = re.compile(sep)<EOL>def _re_line_gen(f, sep):<EOL><INDENT>for line in nopen(f):<EOL><INDENT>yield sep.split(line.rstrip("<STR_LIT:\r\n>"))<EOL><DEDENT><DEDENT>line_gen = _re_line_gen(fname, sep)<EOL><DEDENT><DEDENT>if skip_while:<EOL><INDENT>from itertools import chain<EOL>l = next(line_gen)<EOL>while skip_while(l):<EOL><INDENT>l = next(line_gen)<EOL><DEDENT>line_gen = chain.from_iterable(([l], line_gen))<EOL><DEDENT>if header is namedtuple:<EOL><INDENT>header = next(line_gen)<EOL>header[<NUM_LIT:0>] = header[<NUM_LIT:0>].lstrip("<STR_LIT:#>")<EOL>nt = namedtuple('<STR_LIT>', header)<EOL>for toks in line_gen:<EOL><INDENT>yield nt._make(toks)<EOL><DEDENT>raise StopIteration<EOL><DEDENT>elif callable(header):<EOL><INDENT>for toks in line_gen:<EOL><INDENT>yield header(toks)<EOL><DEDENT>raise StopIteration<EOL><DEDENT>a_dict = dict<EOL>if header == "<STR_LIT>":<EOL><INDENT>try:<EOL><INDENT>from collections import OrderedDict as a_dict<EOL><DEDENT>except ImportError:<EOL><INDENT>from ordereddict import OrderedDict as a_dict<EOL><DEDENT>header = True<EOL><DEDENT>if header is True:<EOL><INDENT>header = next(line_gen)<EOL>header[<NUM_LIT:0>] = header[<NUM_LIT:0>].lstrip("<STR_LIT:#>")<EOL><DEDENT>if header:<EOL><INDENT>for toks in line_gen:<EOL><INDENT>yield a_dict(izip(header, toks))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for toks in line_gen:<EOL><INDENT>yield toks<EOL><DEDENT><DEDENT>
r"""
    for each row in the file `fname` generate dicts If `header` is True
    or lists if `header` is False. The dict keys are drawn from the first
    line.
    If `header` is a list of names, those will be used as the dict keys.
    If `header` is 'ordered', the function will yield ordered dicts.
    If `header` is a callable, it will be called for each row.
    If `header` is collections.namedtuple, a namedtuple object will be yielded
    for each row using the first row as the names.
    skip_while is a function that returns False when it is ready to start
    consuming. this could be something like:

    skip_while = lambda toks: toks[0].startswith('#')

    >>> import sys
    >>> if sys.version_info[0] < 3:
    ...     from StringIO import StringIO
    ... else:
    ...     from io import StringIO
    >>> get_str = lambda : StringIO("a\tb\tname\n1\t2\tfred\n11\t22\tjane")
    >>> expected = [{'a': '1', 'b': '2', 'name': 'fred'},
    ... {'a': '11', 'b': '22', 'name': 'jane'}]
    >>> list(reader(get_str())) == expected
    True

    >>> expected = [['a', 'b', 'name'],
    ...             ['1', '2', 'fred'], ['11', '22', 'jane']]
    >>> list(reader(get_str(), header=False)) == expected
    True