I have a csv file with headers at the top of the data columns as ...
<Header1>, <Header2>, ... ,<HeaderN> <data11> , <data12> , ... ,<data1N> <data21> , <data12> , ... ,<data2N> ... , ... , ... , ... <dataM1> , <dataM2> , ... ,<dataMN>
(i.e. standard tabular data)
When reading this with DictReader I use a nested loop to add items to a line read into a list in the appropriate key, like
f = <path_to_some_csv_file.csv> dr = csv.DictReader(open(f)) dict_of_lists = dr.next() for k in dict_of_lists.keys(): dict_of_lists[k] = [dict_of_lists[k]] for line in dr: for k in dict_of_lists.keys(): dict_of_lists[k].append(line[k])
The first loop sets all the values ββin the dict to an empty list. The next loop iterates over each line read from the csv file, from which DictReader creates a key for keys. The inner loop adds the value to the list corresponding to the corresponding key value, so I end the list of dicts I need. In the end, I have to write this quite often.
My question is, is there a more Pythonic way of doing this using built-in functions without a nested loop, or a better idiom, or an alternative way of storing this data structure so that I can return the index list by querying with a key value? If so, is there also a way to format the data that falls into the front column? (for MWE just copy the above data into a text file and run it through the code) Thanks in advance!