Search code examples
pythonfindposition

Find a string on one line and return the string directly below the found string - Python or UNIX


I am having trouble coming up with a way to parse a file in python. The file has rows with random letters and characters (see below).

ABCDBCADBADCBABDDACAABCDB 
1#JHHHF##@@GGIHHHIII==DDS
ACBDAACBABDCCBBDCDBBADADA
ISHFIDH#H@1JD=@HHII##GG=H
CCCDDAABCCBABBADBADBAAABA
%%$$#@GGTTT&%MHTHWAIIHJWI

Given a string, e.g. 'BAD' return or print the characters immediately below it. For this file, the code should return:

#@@
##G
MHT
HWA

Thanks for any assistance!

Here's what I've started with:

#!/usr/bin/python
f=open('file.txt')
lines=f.readlines()
num_lines = sum(1 for line in open('file.txt'))
x = 0
while x <= num_lines:
    _str = lines[x]
    loc = _str.find('BAD')
    x = x+1
    _str = lines[x]
    print (_str[loc:loc+2]
    x=x+1

Solution

  • You are close to the solution. Step through the pairs of lines, find the marker ("BAD") in the first and the word with the same index in the second:

    marker = 'BAD'
    marker_len = len(marker)
    
    for l1, l2 in zip(lines, lines[1:]):
       i = l1.find(marker) # Find the first marker
       while i != -1: # Are there any more markers?
         print(l2[i:i + marker_len])
         i = l1.find(marker, i + marker_len) # Search the rest of l1
    # #@@
    # ##G
    # MHT
    # HWA