2

I have a text file: flower.txt

content in file:

flower white
flower red
flower green
flower blue
Have some other text to the end of file.

I want to grep the line, that is last occurrence of word "flower" and validate the line if it is "flower blue".

Not sure which function helps doing this.

import re

with open('flower.txt', "r") as f:

will re.find help???

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sai Kiran
  • 37
  • 5
  • 1
    Loop through the file, assigning the line to a variable if it matches `flower`. At the end of the loop the variable contains the last occurrence of `flower`. Then you can test if it matches `flower blue`. – Barmar May 08 '18 at 20:01
  • 1
    No need for a regexp. `"flower" in line` will tell you if the word is in there. – Barmar May 08 '18 at 20:02
  • Can you help me the loop, couldn't get it. – Sai Kiran May 08 '18 at 20:32
  • @SaiKiran Who are you asking? – Anton vBR May 08 '18 at 20:33
  • @SaiKiran Show what you tried, then we can help you fix it. That's how you learn better. – Barmar May 08 '18 at 20:35
  • Sure thanks sir. I'm trying to pull word next to first occurrence of flower which is "white". But below one gets all word from all line that has flower. how can I only get for first occurrence ? – Sai Kiran May 08 '18 at 20:59
  • with open('acl.txt', 'r') as f: for line in f: for i, word in enumerate(line.split()): if word == 'access-list': print line.split()[1] – Sai Kiran May 08 '18 at 20:59

1 Answers1

2

Here you go:

with open('temp.txt','w') as f:
    f.write('''\
flower white
flower red
flower green
flower blue
Have some other text to the end of file.''')

with open('temp.txt') as f:
    flowerlines = [i.strip() for i in f.readlines() if i.startswith('flower')]

if flowerlines:
    print(flowerlines[-1] == 'flower blue')
else:
    print('No flowers found.')

Or You could go like this:

searchW = 'flower blue'
with open('temp.txt') as f:
    flowerlines = next((i == searchW  for i in f.read().split('\n')[::-1] 
                        if i.startswith('flower')),'No flowers')
    print(flowerlines)

If you need to optimize this (and most likely you should not) you could have a look at this: https://stackoverflow.com/a/23646049/7386332

Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • Great. Thanks but If I see any flower keyword after flower blue, the code should fail. But the above one still says true. – Sai Kiran May 08 '18 at 20:09
  • @SaiKiran What do you mean? This code should read all lines and only save those who start with flower. After that it takes the last line and checks if that is flower blue (prints True). If it is not flower blue (prints False) and if no flower ... prints No flowers. – Anton vBR May 08 '18 at 20:10
  • I mean, if i have another line that says flower yellow to the end of file , it should fail. The last one should be flower blue always. – Sai Kiran May 08 '18 at 20:12
  • @SaiKiran Yes, and it will fail if it is flower yellow. We validate it against flower blue. – Anton vBR May 08 '18 at 20:12
  • Still confused working on this. But will try to get exact output. – Sai Kiran May 08 '18 at 20:53
  • Got it. Thank you sir. – Sai Kiran May 08 '18 at 21:25