Hi A text file which has following entries
CHECK>ABC
Battery: OK
RSA: OK
Health Check
CHECK>ABC Verbose
Battery: OK
Battery: voltage 1 2.3v
Battery: voltage 2 2.3v
RSA: OK
Health Check
I would like to get only following from the file
Battery: OK
RSA: OK
I have following code
start=r'^CHECK>ABC$'
end=r'^Health Check'
pattern = r'{p0}(?!.*{p0})(?:.*?{p1}|.*)'.format(p0=start, p1=end)
matches = re.findall(pattern, text, re.DOTALL | re.MULTILINE)
if matches:
last_match = matches[-1]
data.append(last_match.split('\n'))
if not re.search(end, last_match, re.DOTALL | re.MULTILINE):
print("error_no_end")
else:
print("error_no_match")
it gives following information in data
CHECK>ABC Verbose
Battery: OK
Battery: voltage 1 2.3v
Battery: voltage 2 2.3v
RSA: OK
Health Check
I guess, you are interested in Battery status and RSA status only, if yes, then these lines of code shall help you.
data = []
battery_status = re.search(r"\b(Battery)\s*:\s*\b([A-Z]+)\b", text)
data.append(battery_status.group(1) + ": " + battery_status.group(2))
rsa_status = re.search(r"\b(RSA)\s*:\s*\b([A-Z]+)\b", text)
data.append(rsa_status.group(1) + ": " + rsa_status.group(2))
data = "\n".join(data)
print(data)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.