I have a large txt file that is split into lines. I want to load the nth line of this file for use in a for loop in my code. I can't however load the whole array then slice it because of the RAM (the whole file is like 500GB!). Any help would be much appreciated.
Asked
Active
Viewed 157 times
1
-
Would this help: https://stackoverflow.com/questions/2081836/how-to-read-specific-lines-from-a-file-by-line-number – Zachary Apr 14 '21 at 07:53
2 Answers
0
You can use a for-loop to iterate over the lines.
with open("file.txt") as f:
for line in f:
print(line)
When iterating over the file, you only have the current line in memory.

wvdgoot
- 303
- 2
- 10
0
The python enumerate function is your go to here. This is because it goes through all of the data without loading everything into memory.
Example code to load the data from line 26
line = 26
f = open(“file”)
data = None
for i, item in enumerate(f):
if i == line - 1:
data = item
break
if data is not None:
print(data)

Zachary
- 532
- 3
- 12