Last active
February 2, 2022 02:26
-
-
Save adigitoleo/2bbfb2e8a065801eb74e4232452dee4c to your computer and use it in GitHub Desktop.
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
"""Read columns of a delimited file into a namedtuple. | |
Supports column type parsers, e.g. | |
def parser(colname, data): | |
if colname == "foo": | |
return map(float, data) | |
return data | |
""" | |
import csv | |
import collections as c | |
def read_namedtuple(csvfile, column_names=None, parser=None, **kwargs): | |
"""Read columns of a delimited file into nested tuples in a named tuple [1]. | |
Optionally specify column names. If None, attempt to parse column | |
names from the first line of the file. | |
Optionally specify a column parser. The callable must take two | |
positional arguments, which are the column name and the column data, | |
respectively. The callable must return an iterable of the processed | |
column data. | |
Pass any additional keyword arguments to `csv.reader` [2]. | |
[1]: <https://docs.python.org/3/library/collections.html#collections.namedtuple> | |
[2]: <https://docs.python.org/3/library/csv.html#csv.reader> | |
""" | |
with open(csvfile) as file: | |
reader = csv.reader(file, **kwargs) | |
if column_names is None: | |
cols = [s.replace(' ', '_') for s in next(reader)] | |
else: | |
cols = column_names | |
Columns = c.namedtuple("Columns", cols) | |
if parser is None: | |
return Columns._make([tuple(x) for x in zip(*list(reader))]) | |
return Columns._make([tuple(parser(c, x)) for c, x in zip(cols, zip(*list(reader)))]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment