0

I want use python to read specific multiply lines from txt files. For example ,read line 7 to 10, 17 to 20, 27 to 30 etc.

Here is the code I write, but it will only print out the first 3 lines numbers. Why? I am very new to use Python.

with open('OpenDR Data.txt', 'r') as f:  
    for poseNum in range(0, 4):               
        Data = f.readlines()[7+10*poseNum:10+10*poseNum] 
        for line in Data: 
            matAll = line.split()    
            MatList = map(float, matAll) 
            MatArray1D = np.array(MatList)
            print MatArray1D
Kara
  • 6,115
  • 16
  • 50
  • 57
SimaGuanxing
  • 673
  • 2
  • 10
  • 29
  • 2
    Possible duplicate of [Reading specific lines only (Python)](http://stackoverflow.com/questions/2081836/reading-specific-lines-only-python) – Two-Bit Alchemist Nov 12 '15 at 21:40
  • You're trying to read the file over and over again. Besides being inefficient, it won't work unless you rewind the file each time. Read the file before the loop. – saulspatz Nov 12 '15 at 21:44
  • @saulspatz Hi, thanks. I use 7+10*poseNum:10+10*poseNum to control reading different rows in every loop, why this is not working? – SimaGuanxing Nov 12 '15 at 21:46
  • `readlines()` reads the whole file at once. Each time you hit the line `Data = f.readlines()[7+10*poseNum:10+10*poseNum] ` you try to read the file again. Reading the file and retrieving and retrieving specific rows from the list are two distinct operations. – saulspatz Nov 12 '15 at 21:50

4 Answers4

2
with open('OpenDR Data.txt') as f:
    lines = f.readlines()

for poseNum in range(0, 4):               
    Data = lines[7+10*poseNum:10+10*poseNum]
Hasan Ramezani
  • 5,004
  • 24
  • 30
2

This simplifies the math a little to choose the relevant lines. You don't need to use readlines().

with open('OpenDR Data.txt', 'r') as fp:
    for idx, line in enumerate(fp, 1):
        if idx % 10 in [7,8,9,0]:
            matAll = line.split()    
            MatList = map(float, matAll) 
            MatArray1D = np.array(MatList)
            print MatArray1D
vk1011
  • 7,011
  • 6
  • 26
  • 42
1

You should only call readlines() once, so you should do it outside the loop:

with open('OpenDR Data.txt', 'r') as f:  
    lines = f.readlines()
    for poseNum in range(0, 4):               
        Data = lines[7+10*poseNum:10+10*poseNum] 
        for line in Data: 
            matAll = line.split()    
            MatList = map(float, matAll) 
            MatArray1D = np.array(MatList)
            print MatArray1D
wvdz
  • 16,251
  • 4
  • 53
  • 90
0

You can use a combination list slicing and comprehension.

start = 7
end = 10
interval = 10
groups = 3

with open('data.txt') as f:
    lines = f.readlines()
    mult_lines = [lines[start-1 + interval*i:end + interval*i] for i in range(groups)]

This will return a list of lists containing each group of lines (i.e. 7 thru 10, 17 thru 20).

hawkeye
  • 159
  • 3