Most elegant way to break CSV columns into separate data structures using Python?
Posted
by Nick L
on Stack Overflow
See other posts from Stack Overflow
or by Nick L
Published on 2010-05-04T20:55:14Z
Indexed on
2010/05/04
20:58 UTC
Read the original article
Hit count: 243
I'm trying to pick up Python. As part of the learning process I'm porting a project I wrote in Java to Python. I'm at a section now where I have a list of CSV headers of the form:
headers = [a, b, c, d, e, .....]
and separate lists of groups that these headers should be broken up into, e.g.:
headers_for_list_a = [b, c, e, ...]
headers_for_list_b = [a, d, k, ...]
. . .
I want to take the CSV data and turn it into dict's based on these groups, e.g.:
list_a = [
{b:val_1b, c:val_1c, e:val_1e, ... },
{b:val_2b, c:val_2c, e:val_2e, ... },
{b:val_3b, c:val_3c, e:val_3e, ... },
. . .
]
where for example, val_1b is the first row of the 'b' column, val_3c is the third row of the 'c' column, etc.
My first "Java instinct" is to do something like:
for row in data:
for col_num, val in enumerate(row):
col_name = headers[col_num]
if col_name in group_a:
dict_a[col_name] = val
elif headers[col_cum] in group_b:
dict_b[col_name] = val
...
list_a.append(dict_a)
list_b.append(dict_b)
...
However, this method seems inefficient/unwieldy and doesn't posses the elegance that Python programmers are constantly talking about. Is there a more "Zen-like" way I should try- keeping with the philosophy of Python?
© Stack Overflow or respective owner