Alexandra Alexandra - 29 days ago 18
Python Question

Dictionary from file only shows last line

I have a .txt file with 994 rows and 7 columns. The second column has got the name of a protein residue that interacts with the residue on the 3rd column. The rest of them to the right are scores (the first two don't matter).

I am trying to turn that into a dictionary with the elements on the 2nd column as the key and the rest as the key values. The problem is it only seems to have put in the last line.

This is my code:

>>> f=open('C:/Users/Alex/Documents/1TRKGremlin.txt')
>>> dict={}
>>> for line in f:
... lsplit=line.split()

>>> try:dict[lsplit[2]].appendl(lsplit[3:])
... except KeyError: dict[lsplit[2]]=[lsplit[3:]]

>>>print dict[]#here I only get the last line of the file


The file looks like this:

i j i_id j_id r_sco s_sco prob
205 208 205_K 208_E 0.5625 3.889 1.000
557 660 557_I 660_A 0.5471 3.783 1.000
425 439 425_M 439_G 0.5462 3.776 1.000
19 76 19_A 76_S 0.4867 3.365 1.000
436 462 436_K 462_P 0.4770 3.298 1.000
579 661 579_K 661_Q 0.4446 3.074 1.000


Thank you!

Answer

Your for loop is running to completion before you set anything in the dict. The dict entries must be initialized in the loop, because once the loop is finished, you only have the value of lsplit for the last line:

>>> f = open('C:/Users/Alex/Documents/1TRKGremlin.txt')
>>> mydict = {}
>>> for line in f:
...    lsplit = line.split()
...    try:
...        mydict[lsplit[2]].append(lsplit[3:])
...    except KeyError:
...        mydict[lsplit[2]] = [lsplit[3:]]
...
>>> print mydict

Side-note: I renamed your dictionary to mydict. Never name a variable the same name as a Python built-in or you'll shadow the built-in, making it unusable for the scope of the variable, and causing confusing errors. I also fixed some typos and added spaces around assignment operators per PEP8.