parsing - Splitting a file into a dictionary in Python -


i'm beginner python user apologies if rather simple question. have file containing 2 lists divided tab. store in dictionary, each entry associated corresponding entry after tab, such that:

cat hat mouse bowl rat nose monkey uniform dog whiskers elephant dance 

would divided into

{'cat'; 'hat', 'mouse' ; 'bowl') etc. etc.  

it's long list.

this tried:

enhancertad = open('tad_to_enhancer.map', 'r') list = enhancertad.split()  entry in list:     key, val = entry.split('\t')     et[key] = val  print et 

here's recent attempt, , error message below:

enhancertad = open('tad_to_enhancer.map', 'r').read() et = {} lst = enhancertad.split("\n") entry in lst:   key, val = entry.strip().split(' ',1)   et[key] = val  enhancergene = open('enhancer_to_gene_map.txt', 'r').read() ge = {} lst1 = enhancergene.split("\n") entry in lst1:   key, val = entry.strip().split(' ',1)   ge[key] = val  genetad = open('tad_to_gene_map.txt', 'r').read() gt = {} lst2 = genetad.split("\n") entry in lst2:   key, val = entry.strip().split(' ',1)   gt[key] = val 

file "enhancertadmaybe.py", line 13, in key, val = entry.strip().split(' ',1) valueerror: need more 1 value unpack

you can try:

with open('foo.txt', 'r') f:     print dict(line.strip().split('\t', 1) line in f) 

result:

{'monkey': 'uniform', 'dog': 'whiskers', 'cat': 'hat', 'rat': 'nose', 'elephant': 'dance', 'mouse': 'bowl'} 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -