开发者

How to get two line above a selected string?

Say i had text like this:

www.something.com

get the 2 above

www.somethingss.com

get the 2 above

www.somethingssss.com

get the 2 above

Maybe something like this:

for a in text:
    if 'get the 2 above' in a:
        get 2 above 

How would i do开发者_JAVA技巧 this?


Assuming the above is a string:

s = '''www.something.com

get the 2 above

www.somethingss.com

get the 2 above

www.somethingssss.com

get the 2 above'''

s = s.splitlines()
for i, line in enumerate(s):
    if 'get the 2 above' in line:
        print s[i-1], s[i-2]

s.splitlines() removes the newlines; if you want to leave them in, pass True, as in s.splitlines(True) (thanks Sven Marnach).

To get a list of lines from a file, you can just do this:

with open('myfile.txt', 'r') as f:
    s = f.readlines()


You could use an itertools recipe to iterate over fixed-length "windows" on your iterable for this:

import itertools

def windows(iterable, length=2):
    # If iterable is a list, this is equivalent to
    # (iterable[i:i+length] for i in range(len(iterable)-length+1))
    return itertools.izip(*(itertools.islice(it,n,None)
               for n,it in enumerate(itertools.tee(iterable,length))))

text='''\
www.something.com

get the 2 above

www.somethingss.com

get the 2 above

www.somethingssss.com

get the 2 above
'''.splitlines()    

for lines in windows(text,3):
    if lines[2]=='get the 2 above':
        print(lines[0])

# www.something.com
# www.somethingss.com
# www.somethingssss.com


Easy hack: reverse the list. Then you're iterating in the right order which is easy:

lines = iter(reversed(s.splitlines()))
for line in lines:
    if <whatever>:
        next(lines)
        yield next(lines)

(EDIT: fixed brain fart, thanks @Sven)


text = '''www.something0.com
get the 2 above

www.somethingss1.com

get the 2 above

www.somethingssss2.com

get the 2 above



www.somethingss3.com

get the 2 above

www.somethingss4.com
www.somethingss5.com

get the 2 above'''

from collections import deque
above = deque(maxlen=2)
for x in text.splitlines():
    if 'get the 2 above' in x:
        print above
    elif x:
        above.append(x)


print '\n=================\n'

# Senderle's code:

s = text.splitlines()
for i, line in enumerate(s):
    if 'get the 2 above' in line:
        print s[i-1], s[i-2]

result

deque(['www.something0.com'], maxlen=2)
deque(['www.something0.com', 'www.somethingss1.com'], maxlen=2)
deque(['www.somethingss1.com', 'www.somethingssss2.com'], maxlen=2)
deque(['www.somethingssss2.com', 'www.somethingss3.com'], maxlen=2)
deque(['www.somethingss4.com', 'www.somethingss5.com'], maxlen=2)

=================

www.something0.com get the 2 above
 www.somethingss1.com
 www.somethingssss2.com
 www.somethingss3.com
 www.somethingss5.com
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜