Friday, May 29, 2009

Lookup file in python using dictionary


Input files:
- main.txt contains id:name details
- lkfile contains the result of a particular exam in the format pass/fail:id

$ cat main.txt
id341:Mr X
id990:Mr Y
id223:Mr P
id212:Mr N
id183:Mr L

$ cat lkfile
fail:id223
pass:id341
fail:id183
pass:id990
pass:id212
pass:id555

Required:
Update main.txt with the results from lkfile i.e. required output:

pass:Mr X
pass:Mr Y
fail:Mr P
pass:Mr N
fail:Mr L

The python script using python Dictionaries:

def lookupf(file1,file2,outfile):
fp = open(outfile,"w")
a={}
for line in open(file1):
f = line.strip().split(":")
a[f[1]]=f[0]

for line2 in open(file2):
f2 = line2.strip().split(":")
if len(f2) == 2:
if a.has_key(f2[0]):
fp.write(a[f2[0]] + ":" + f2[1]+"\n")
else:
fp.write(line2.strip())
fp.close()

#Calling the function
lookupf("lkfile","main.txt","out.txt")

Executing:

$ python lookup.py
$ cat out.txt
pass:Mr X
pass:Mr Y
fail:Mr P
pass:Mr N
fail:Mr L

Related concepts:

- The awk alternative would be:

$ awk '
BEGIN {FS=OFS=":"}
NR==FNR{a[$2]=$1;next}a[$1]{$1=a[$1]}1
' lkfile main.txt

- More about python dictionaries
- Python mapping type has_key

0 Comments: