Kadir
Kadir

Reputation: 1615

Storing each column in a separate dictionary using python

Is there an efficient way to store each column of a tab-delimited file in a separate dictionary using python?

A sample input file: (Real input file contains thousands of lines and hundreds of columns. Number of columns is not fixed, it changes frequently.)

A B C
1 4 7
2 5 8
3 6 9

I need to print values in column A:

for cell in mydict["A"]:
    print cell

and to print values in the same row:

for i in range(1, numrows):
    for key in keysOfMydict:
        print mydict[key][i]

Upvotes: 1

Views: 1296

Answers (2)

thavan
thavan

Reputation: 2429

Not sure this is relevant, but you can do this using rpy2.

from rpy2 import robjects
dframe = robjects.DataFrame.from_csvfile('/your/csv/file.csv', sep=' ')
d = dict([(k, list(v)) for k, v in dframe.items()])

output:

{'A': [1, 2, 3], 'C': [7, 8, 9], 'B': [4, 5, 6]}

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174624

The simplest way is to use DictReader from the csv module:

with open('somefile.txt', 'r') as f:
   reader = csv.DictReader(f, delimiter='\t')
   rows = list(reader) # If your file is not large, you can
                       # consume it entirely

   # If your file is large, you might want to 
   # step over each row:
   #for row in reader:
   #    print(row['A'])

for row in rows:
   print(row['A'])

@Marius made a good point - that you might be looking to collect all columns separately by their header.

If that's the case, you'll have to adjust your reading logic a bit:

from collections import defaultdict
by_column = defaultdict(list)

for row in rows:
   for k,v in row.iteritems():
       by_column[k].append(v)

Another option is pandas:

>>> import pandas as pd
>>> i = pd.read_csv('foo.csv', sep=' ')
>>> i
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
>>> i['A']
0    1
1    2
2    3
Name: A, dtype: int64

Upvotes: 1

Related Questions