0

I am learning python.i have a text file named test.txt that contain data like below

10  20
20  15
15  12
47  25

and i want to read one row at a time(read [10,20] in first iteration, then [20,15] in second iteration) and want to do some execution .Thus i wrote a test code as below

import numpy as np
x1=np.loadtxt('test.txt')
for rows in x1:
    y=rows[:]
    print(y)

but it prints whole content of the text file...can anyone help me reading a row at each iteration of the for loop ? Thanks.

  • Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – jrmylow Sep 25 '20 at 13:12

2 Answers2

0

I ran your code and this is the output I got:

[10. 20.]
[20. 15.]
[15. 12.]
[47. 25.]

Each iteration in the for loop prints the ith row and then goes back to a new line. So here, the first iteration printed [10. 20.], the second one printed [20. 15.],etc... So your code is already doing what you want it to do. Now, instead of printing each row every time, do the work you need to do with each row like this:

import numpy as np
x1=np.loadtxt('test.txt')
for rows in x1:
    y=rows[:] #y will contain the ith row data
    PROCESS_FOR_THE_ith_ROW(y) 

EDIT 1: answering your comment, yes it is possible. Here's what you can do:

import numpy as np
x1=np.loadtxt('test.txt')
i=0
for rows in x1:
    i=i+1
    y=rows[:] #y will contain the ith row data
    with open(f"text{i}.txt",'w') as f:
        f.write(str(y))
Ali Doggaz
  • 105
  • 1
  • 6
  • is it possible to write the output of each iteration to separate text file with extension iteration number –  Sep 25 '20 at 11:33
  • Yes it is, I just edited my answer to answer your comment. – Ali Doggaz Sep 25 '20 at 11:39
  • if we have many variables like y then does it require to change the line f.write(str(y)) with f.write(str(y1,y2,y3,y4,y5)) –  Sep 25 '20 at 11:50
  • No need to do that, f.write(str(y)) will print all the variables in a list format. – Ali Doggaz Sep 25 '20 at 11:52
0

The numpy array stores the data in the form of rows and columns. If you want to iterate over each row, I would encourage you to look for iter() function. A for loop is made up of iter() and next(). The following code would make things much simpler

Let's say you have the data stored in your array x1 :

You make an iterator which will loop over your array one by one..

a=iter(x1)

Now that the iterator object is made, you have to call the method next() present in your variable 'a'.

next(a) 

You will automatically start getting each row as you further call next(a). You can even assign it to a variable and play around with it..

So your code would be something like :-

import numpy as np
x1=np.loadtxt('test.txt')
a=iter(x1)
first_row=next(a)
second_row=next(a)

There you have it, the second row all by yourself. Use index slicing second_row[0] and second_row[1] to get respective values.

halfer
  • 19,824
  • 17
  • 99
  • 186
Raghav Gupta
  • 454
  • 3
  • 12