Hello, I have a problem while reading numbers from a text file. Let's say I have
1 14 25 12 9 6
written in a text file, and I want python to read those numbers as integers. So when I read and make a list by splitting them [1, 14, 25, 12, 9, 6] I wanna use these numbers for addition/substraction, but Python reads them as str, not int. How to make it read them as integers? Thanks.
Reading integer from a file
Started by your_rock, Oct 30 2010 07:35 AM
2 replies to this topic
#1
Posted 30 October 2010 - 07:35 AM
|
|
|
#2
Posted 30 October 2010 - 02:34 PM
You use int():
theFile = open("filename.txt", "r")
theInts = []
for val in theFile.read().split():
theInts.append(int(val))
theFile.close()
Then just use theInts.
Wow I changed my sig!
#3
Posted 30 October 2010 - 02:59 PM
Thank you very much, it is working now. :)


Sign In
Create Account

Back to top









