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.
Here’s one way:
By doing
for line in fyou are reading bit by bit (using neitherread() allnorreadlines). 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.