使用python从文本文件中提取特定行

时间:2017-11-20 10:13:53

标签: python arrays extract lines

我有大文本文件,我想使用数组

提取特定行

这是行号

的数组

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

所以我想提取这行和之前的100行和

之后的100行

例如:从第76行提取到第276行,删除第1行到第75行,从277到494,从第495行提取到第695行

谢谢! **#UPDATE * 这是我的新代码

它的工作正常,但问题输出打印行号不是行

  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)  

解决

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)  

1 个答案:

答案 0 :(得分:0)

如果您不想在内存中加载所有文件,可以使用以下方法并在结果文件中写入所需的行:

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()

然后你可以从磁盘中删除初始文件:

import os
os.remove('input')