Search code examples
pythonarraysextractlines

Extract specific lines from text file using python


I have large text file and I want to extract specific lines using array

this the array of lines number

[ 176, 595, 1001, 1377, 1736, 2091, 2459, 2860, 3318, 3746, 4179, 4601]

So I want to extract this lines and 100 lines before and 100 lines after

for example : extract from line 76 to line 276 and Delete line 1 to 75 and from 277 to 494 and extract from line 495 to line 695

Thank You!
**#UPDATE* this is my new code

its work fine but the problem output print line number not value in line

  import sys
sys.stdout = open('log.txt', 'w')

lineArray=[176,595,1001,1377,1736,2091,2459,2860,3318,3746,4179,4601]
with open('output.txt', 'w') as output:
    fp = open("ecg.txt")
    fileLines = {}
    for i, line in enumerate(fp):
        fileLines[i] = line
    for lineNo in lineArray:
        for i, line in fileLines.iteritems():
            if i <= (lineNo + 100) and i >= (lineNo - 100):
                print(i)
                #output.write(line)  

SOLVED

import sys
sys.stdout = open('log.txt', 'w')

lineArray=[369,1133,1908,2709,3570,4469,5336,6165,6931,7662,8354,9021,9667,10330,11016,11893,12811,13708,14602,15498,16388,17252,18060,18876,19683,20455,21259,22057,22900,23796,24640,25486,26287,27073,27801,28516,29229,29948,30660,30800,31369,32079,32907,33665,34389,35087,35782,36493,37282,38077,38862,39610,40321,41004,41676,42340,43013,43740,44594,45513,46399,47268,48243,49211,50198,51147,52119,53174,54241,55202,56098,57012,57992,59019,60036,60968,61867,62796,63681,64520,65357]
with open('output.txt', 'w') as output:
    fp = open("ecg.txt")
    fileLines = {}
    for i, line in enumerate(fp):
        fileLines[i] = line
    for lineNo in lineArray:
        for i, line in fileLines.iteritems():
            if i <= (lineNo + 185) and i >= (lineNo - 185):
                print(line)
                #output.write(line)  

Solution

  • if you don't want to load all the file in memory you can use the method below and write required lines in a result file :

    with open('result', 'w') as output:
        fp = open("input")
        for lineNo in lineArray:
            for i, line in enumerate(fp):
                if i < lineNo + MARGIN and i > lineNo - MARGIN :
                    output.write(line)             
        fp.close()
    

    then you can delete the initial file from disk:

    import os
    os.remove('input')