Python Forum
Searching string in file and save next line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching string in file and save next line
#2
this is not tested, but should work:
nxline = False
with open('file.txt') as fp:
    for line in fp:
        line = line.strip()
        if nxline:
            print(f"Got a number: {line}")
            nxline = False
        else:
            if 'Line' in line:
                nxline = True
Reply


Messages In This Thread
RE: Searching string in file and save next line - by Larz60+ - Jul-09-2020, 09:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 437 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 648 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 901 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  save values permanently in python (perhaps not in a text file)? flash77 8 1,429 Jul-07-2023, 05:44 PM
Last Post: flash77
  splitting file into multiple files by searching for string AlphaInc 2 1,083 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,922 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Save and Close Excel File avd88 0 3,422 Feb-20-2023, 07:19 PM
Last Post: avd88
  Getting last line of each line occurrence in a file tester_V 1 971 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Save multiple Parts of Bytearray to File ? lastyle 1 1,028 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,558 Sep-27-2022, 01:38 PM
Last Post: buran

Forum Jump:

User Panel Messages

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