Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract all lines after word

I want to extract all lines that will be printed after this marker: PossibleErrs

s1 = (
 ' DIMM Error Summary\r\n'
 ' Skt  Chan Dimm Slot    DimmSN   KnownErrs PossibleErrs\r\n'
 '    0    5    0 DIMM_F1 XD12F         0            2\r\n'
 '    0    5    1 DIMM_F2 XD12C         0            2\r\n')

My attempt is insufficient because it just returns a single line, when there could be more than one line to process. It also returns the linebreaks \r\n.

re.findall(r'PossibleErrs(\s*.*) [^\r\n]', s1)

How can I parse this text to just return the values found after the marker PossibleErrs?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected:

0, 5, 0, DIMM_F1, XD12F, 0, 2,
0, 5, 1, DIMM_F2, XD12C, 0, 2

>Solution :

You don’t need a regular expression to do this: you just need to split on the CRLFs (\r\ns) to get a list of lines (slicing off the first two lines and the last line), and then split each line on whitespace:

lines = s1.split("\r\n")[2:-1]
result = [line.split() for line in lines]
print(result)

This outputs:

[['0', '5', '0', 'DIMM_F1', 'XD12F', '0', '2'], ['0', '5', '1', 'DIMM_F2', 'XD12C', '0', '2']]

from which you can easily massage it into any desired output format.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading