检索字符的侧翼区域

Retrieving character's flanking region

我试图在一行中定位特定字符 "r",然后检索它两侧的 35 个字符。可能有多个 "r" 所以我试图获得所有这些。我一直在尝试这段代码,但我只得到 headers 并且我无法弄清楚。有什么建议吗?

fhand=open("input.txt")
target = open ("output.txt", "a")
for line in fhand:
    name, id, seq= line.split("\t")
    while atpos < len(seq):
        if atpos == -1:
            break
        atpos = seq.find ("r")
        seq2 = seq[(atpos-35):(atpos+36)]
        line2= name + "\t"+ id + "\t" + seq2 + "\n"
        target.write(line2)
        atpos += 1

print ("Sequences obtained successfully")
target.close()
import csv

with open("input.txt") as infile, open('output.txt', 'w') as fout:
    outfile = csv.writer(fout, delimiter='\t')
    for name, id, seq in csv.reader(infile, delimiter='\t'):
        locs = [i for i,char in enumerate(seq) if char=='r']
        for loc in locs:
            outfile.writerow([name, id, seq[max(loc-35, 0) : loc+36]])