0

I have the following type of data in a text file.

15  1  
23  0  
39 -1  
71 -1  
79  1  
95  1  
127 -2  
151  2  
183  1  
191 -1  
239  0  
247  3  

I want to create a 2d list from the text file as follows. I am able to do that with the code given below with the following result

[[15,  1],
[23,  0], 
[39, -1],  
[71, -1],  
[79,  1],
[95,  1], 
[127, -2],  
[151,  2],  
[183,  1], 
[191, -1],  
[239, 0],  
[247, 3]]  

The problem is that the file contains millions of such data. What do I do to just copy first say 1000 lines at a time? How can I add the conditional statement into my code.

with open("path.text") as file:
    R = [[int(x) for x in line.split()] for line in file]
Kumarm
  • 191
  • 9
  • Possible duplicate of [Read first N lines of a file in python](https://stackoverflow.com/questions/1767513/read-first-n-lines-of-a-file-in-python) – Hamms Jul 25 '17 at 23:27
  • @Hamms Its not. I tried their solutions their it does not work for 2d array. – Kumarm Jul 25 '17 at 23:30
  • Why are you reading it beforehand instead of processing it on the fly? – Ignacio Vazquez-Abrams Jul 25 '17 at 23:30
  • @IgnacioVazquez-Abrams I am not reading it. Its part of big computation and I need to do it in stages taking may be 10^6 at a time. – Kumarm Jul 25 '17 at 23:32
  • It is definitely a duplicate of that, you just need to adapt the answer to include the little bit of processing you're doing. `with open("path.text") as file: R = [[int(x) for x in next(file).split()] for x in xrange(N)]` – Hamms Jul 25 '17 at 23:35
  • @Hamms Thanks.. This works. I tried to modify accordingly for almost an hour but was unsuccessful. – Kumarm Jul 25 '17 at 23:39

1 Answers1

0

Here is a way of doing that, not sure if it's the best.

with open("test2.txt") as file:
    R = []
    while len(R) < 1000:
        R.append([int(x) for x in file.readline().split()])
jacoblaw
  • 1,263
  • 9
  • 9