How do I load specific rows from a .txt file in Python?
Given this file:
1,2,3
4,5,6
7,8,9
10,11,12
13,14,15
16,17,18
19,20,21
You can use the csv module to get the desired np array:
import csv
import numpy as np
desired=[1,3,5]
with open('/tmp/test.csv', 'r') as fin:
reader=csv.reader(fin)
result=[[int(s) for s in row] for i,row in enumerate(reader) if i in desired]
print(np.array(result))
Prints:
[[ 4 5 6]
[10 11 12]
[16 17 18]]
Just to expand on my comment
$ cat file.txt
line 0
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
Python:
#!/usr/bin/env python
a = [1, 4, 8]
with open('file.txt') as fd:
for n, line in enumerate(fd):
if n in a:
print line.strip()
output:
$ ./l.py
line 1
line 4
line 8