Created
January 19, 2015 17:09
-
-
Save themiurgo/41c5b5d68474f91350f4 to your computer and use it in GitHub Desktop.
csvtable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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