tags:

views:

51

answers:

1

If I have a file of pairs of integer IDs, followed by a value, I'd like to create this into a dictionary. Each separate term is separated by a newline. I want to make sure these are all held as ints. How can I do this?

edit: as requested, a sample.

9 120
10 12
11 4
12 1
13 515
14 32
+3  A: 
d={}
f=open("file")
for line in f:
    a,b=map( int, line.split() ) 
    d[a]=b
f.close()
print d

output

$ cat file
9 120
10 12
11 4
12 1
13 515
14 32

$ ./python.py
{9: 120, 10: 12, 11: 4, 12: 1, 13: 515, 14: 32}
ghostdog74
This would hold them as strings though, correct? Is there a way to force them as ints? edit: nevermind. this looks great. thanks
Mark
yes, you can use int()
ghostdog74