Python Forum
Help to find a string and read the next lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help to find a string and read the next lines
#4
Here it may be easier to write an regex.
Also bye using compile and finditer make it efficient for larger files.
pattern = re.compile(r"\[DOSIMETRY_TOTAL.*\]\s+(\S+)")
for match in pattern.finditer(data):
    print(20 * '-')
    print(match.group(0)) 
Output:
[DOSIMETRY_TOTAL_DOSE_B: 00] 9.30988 -------------------- [DOSIMETRY_TOTAL_DOSE_E: 00] 8.45142 -------------------- [DOSIMETRY_TOTAL_DOSE_B: 01] 9.18214 -------------------- [DOSIMETRY_TOTAL_DOSE_E: 01] 8.41000 -------------------- [DOSIMETRY_TOTAL_DOSE_B: 02] 8.87531 -------------------- [DOSIMETRY_TOTAL_DOSE_E: 02] 8.35574 .....
data that i test with is just string of the whole file.
group(1) will be values only.
Output:
9.30988 -------------------- 8.45142 -------------------- 9.18214 -------------------- 8.41000 -------------------- 8.87531 -------------------- 8.35574 -------------------- 9.15688 -------------------- 8.40126 -------------------- 8.88971 -------------------- 8.48842 .....
Reply


Messages In This Thread
RE: Help to find a string and read the next lines - by snippsat - Mar-19-2020, 06:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,400 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  read a text file, find all integers, append to list oldtrafford 12 3,748 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Find and Replace numbers in String giddyhead 2 1,284 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Editing text between two string from different lines Paqqno 1 1,343 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,192 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,278 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  [Solved] Trying to read specific lines from a file Laplace12 7 3,622 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Find string between two substrings, in a stream of data xbit 1 2,191 May-09-2021, 03:32 PM
Last Post: bowlofred
  reading lines from a string [Solved] ebolisa 14 6,504 Mar-28-2021, 08:16 PM
Last Post: perfringo
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,457 Jan-15-2021, 04:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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