Skip to content

Instantly share code, notes, and snippets.

@dvska
Created January 30, 2018 15:46
Show Gist options
  • Select an option

  • Save dvska/b60184d8c2f4b3224a33ffb3c462530b to your computer and use it in GitHub Desktop.

Select an option

Save dvska/b60184d8c2f4b3224a33ffb3c462530b to your computer and use it in GitHub Desktop.

Revisions

  1. dvska created this gist Jan 30, 2018.
    30 changes: 30 additions & 0 deletions oocsv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    """OOcsv: object view of CSV
    ~5 times faster than OODict
    2013 Dmitry V. Selitsky [email protected] / dvska-at-skype
    """
    from collections import namedtuple

    def OOcsv(csv_file_obj, use_unicodecsv=False, **kwargs):

    if use_unicodecsv:
    import unicodecsv as csv
    else:
    import csv

    head = next(csv_file_obj).replace(' ', '_').replace('"', '').replace(';', ',')
    csv_data = csv.reader(csv_file_obj, **kwargs)
    Record = namedtuple('Record', head, rename=True)
    #for oo_row in (Record._make(x) for x in csv_data):
    # yield oo_row
    return (Record._make(x) for x in csv_data)

    # Usage:
    #
    # with open("Batting.csv") as f:
    # for r in OOcsv(f):
    # if r.yearID=='1977': pass

    # pprint( tuple(
    # OOcsv(StringIO(raw_csv.encode('utf8')),
    # delimiter=';', quoting=csv.QUOTE_ALL)))