with open("polish_wiki_excerpt.in", encoding="utf-8") as f: lines = f.readlines() for line in lines: line = line.strip() line = line.split() flag1 = False # sprawdzam czy slowo jest liczba miedzy 1900 a 1999 flag2 = False # sprawdzam czy za liczba jest 'r.' for word in line: if word == 'Terespola': pass if flag1: if word[:2] == 'r.': flag2 = True break else: flag1 = False if word[len(word)-4 : len(word)-2] == '19': if word[2] in '0123456789' and word[3] in '0123456789': flag1 = True if flag1 is True and flag2 is True: goodLine = ' '.join(line) print(goodLine)