Skip to content

Instantly share code, notes, and snippets.

@themiurgo
Created January 19, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themiurgo/41c5b5d68474f91350f4 to your computer and use it in GitHub Desktop.
Save themiurgo/41c5b5d68474f91350f4 to your computer and use it in GitHub Desktop.
csvtable
import csv
import os
from collections import namedtuple
def read_csv(fname, tabname=None, names=None, headers=True):
with open(fname, "r") as fobj:
if not tabname:
full_basename = os.path.basename(fname)
basename, ext = os.path.splitext(full_basename)
tabname = basename.capitalize()
if not names and headers:
names = fobj.read().strip()
Row = namedtuple(tabname, names)
reader = csv.reader(fobj)
for row in reader:
yield Row(*row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment