read numbers from file and covert to integer

beliavsky at aol.com beliavsky at aol.com
Mon Jan 10 00:40:15 CET 2005


If you have a file "ints.txt" with contents

10 20
30
40 50 60

You could read integers into a list with

ivec = []
for text in open("ints.txt","r"):
words = text.split()
for x in words:
ivec.append(int(x))
print ivec

If you know you want to read two integers into variables a,b from a
line you could write

a,b = int(words[0]),int(words[1])

Excuse me, but if your question is really so elementary that this post
answered it, you should probably read a book or tutorial.




More information about the Python-list mailing list