How can I read int from a file? I have a large(512MB) txt file, which contains integer data as:
0 0 0 10 5 0 0 140
0 20 6 0 9 5 0 0
Now if I use c = file.read(1)
, I get only one character at a time, but I need one integer at a time. Like:
c = 0
c = 10
c = 5
c = 140 and so on...
Any great heart please help. Thanks in advance.
Summary: To read inputs from a user and convert it to an integer, use the command int(input('Prompt')) for Python 3 and input('Prompt') for Python 2.
To only allow integer user input: Use a while True loop to loop until the user enters an integer. Use the int() class to attempt to convert the value the user entered to an integer. If the user entered an integer, use the break statement to break out of the loop.
One solution is to use raw_input() two times. Note that we don't have to explicitly specify split(' ') because split() uses any whitespace characters as a delimiter as default.
As for why 1.0 == 1 , it's because 1.0 and 1 represent the same number. Python doesn't require that two objects have the same type for them to be considered equal.
512 MB is really not that large. If you're going to create a list of the data anyway, I don't see a problem with doing the reading step in one go:
my_int_list = [int(v) for v in open('myfile.txt').read().split()]
if you can structure your code so you don't need the entire list in memory, it would be better to use a generator:
def my_ints(fname):
for line in open(fname):
for val in line.split():
yield int(val)
and then use it:
for c in my_ints('myfile.txt'):
# do something with c (which is the next int)
Here's one way:
with open('in.txt', 'r') as f:
for line in f:
for s in line.split(' '):
num = int(s)
print num
By doing for line in f
you are reading bit by bit (using neither read() all
nor readlines
). Important because your file is large.
Then you split each line on spaces, and read each number as you go.
You can do more error checking than that simple example, which will barf if the file contains corrupted data.
As the comments say, this should be enough for you - otherwise if it is possible your file can have extremely long lines you can do something trickier like reading blocks at a time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With