Python Forum
print only last matched line
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print only last matched line
#16
Interesting. Other than a typo in line 18 (last_lstr, not last_str) it works fine for me. I wrote a little program that generated a file of 1000 sentences with lstr and lstart randomly sprinkled throughout. It found each LotComplete/-- InitializeRequest pair and printed the line numbers to stdout.

None is a special Python object that is often used to create a variable or attribute but not really assign a value. Below I use None to make the variable x and indicate that x doesn't have a real value.
x = None  # Want to make x, but not give it a real value

… some time later...

if x is None:  # If x not assigned a value, assign one now.
    x = 10
The line "if x is None:" is a lot like asking "if x == None:", but instead of comparing values for equivalency, it checks if the object ID of the thing in x is the same the same as None's object ID. I think this may be a holdover form olden times when you could actually change the value of things like True and False in Python. Imagine what would happen if you were allowed to do this:
False = True  # Make False and True have the same value
x = True
if x == True:
    print('x == True')
if x == False:
    print('x == False')
if x is False:
    print('x is False')
Output:
x == True x == False
The last if evaluates to False and the line does not print. Even though x has the same value as False, it is not False. The object ID's don't match.

So back to the question. This test is checking if both last_start and last_lst have been assigned values. If so, that means we saw both of these lines and it is ok to print the pair.
if last_lstart is not None and last_lstr is None:
Reply


Messages In This Thread
print only last matched line - by tester_V - Apr-22-2020, 08:07 PM
RE: print only last matched line - by deanhystad - Apr-22-2020, 09:07 PM
RE: print only last matched line - by tester_V - Apr-22-2020, 09:25 PM
RE: print only last matched line - by deanhystad - Apr-22-2020, 10:23 PM
RE: print only last matched line - by tester_V - Apr-22-2020, 11:23 PM
RE: print only last matched line - by deanhystad - Apr-23-2020, 12:37 AM
RE: print only last matched line - by tester_V - Apr-23-2020, 01:49 AM
RE: print only last matched line - by deanhystad - Apr-23-2020, 02:24 AM
RE: print only last matched line - by tester_V - Apr-23-2020, 03:54 AM
RE: print only last matched line - by tester_V - Apr-23-2020, 04:57 AM
RE: print only last matched line - by deanhystad - Apr-23-2020, 01:22 PM
RE: print only last matched line - by tester_V - Apr-23-2020, 05:43 PM
RE: print only last matched line - by tester_V - Apr-23-2020, 06:47 PM
RE: print only last matched line - by deanhystad - Apr-23-2020, 07:20 PM
RE: print only last matched line - by tester_V - Apr-23-2020, 07:57 PM
RE: print only last matched line - by deanhystad - Apr-23-2020, 09:59 PM
RE: print only last matched line - by tester_V - Apr-24-2020, 12:40 AM
RE: print only last matched line - by deanhystad - Apr-24-2020, 02:12 AM
RE: print only last matched line - by tester_V - Apr-24-2020, 05:07 AM
RE: print only last matched line - by tester_V - Apr-27-2020, 04:31 AM
RE: print only last matched line - by deanhystad - Apr-27-2020, 07:50 AM
RE: print only last matched line - by tester_V - Apr-29-2020, 11:38 PM
RE: print only last matched line - by deanhystad - Apr-30-2020, 03:01 AM
RE: print only last matched line - by tester_V - Apr-30-2020, 05:10 AM
RE: print only last matched line - by deanhystad - Apr-30-2020, 05:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 546 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,804 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  failing to print not matched lines from second file tester_V 14 6,496 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,309 Mar-30-2022, 04:14 AM
Last Post: DaveG
  If match not found print last line tester_V 2 2,995 Apr-26-2021, 05:18 AM
Last Post: tester_V
  print a line break in writelines() method leodavinci1990 1 6,678 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Print characters in a single line rather than one at a time hhydration 1 2,137 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 6,071 Sep-30-2020, 02:53 PM
Last Post: buran
  Pattern Require Last Line Print() why? Harshil 4 2,578 Aug-08-2020, 04:54 PM
Last Post: Harshil
  Python re.sub text manipulation on matched contents before substituting xilex 2 2,236 May-19-2020, 05:42 AM
Last Post: xilex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020