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
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}
精彩评论