I have a file like:
Single-device update Received at:Sun Aug 08 2021, 10:33 PM Control unit type:regard Control unit identifier:rdfdf Target:dfdfdf Download start: Sun Aug 08 2021, 10:13:40 PM Download completed: Sun Aug 08 2021, 10:13:41 PM Installation start: Sun Aug 08 2021, 10:15:42 PM Installation completed: Sun Aug 08 2021, 10:33:59 PM Result code OK DFG Response failed: [0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN] Result code OK Device has been successfully installed Single-device update Received at:Sun Aug 08 2021, 10:03 PM Control unit type:regard Control unit identifier:fdfd Target:fdgh Download start: Sun Aug 08 2021, 9:43:19 PM Download completed: Sun Aug 08 2021, 9:43:19 PM Installation start: Sun Aug 08 2021, 9:45:29 PM Installation completed: Sun Aug 08 2021, 10:03:45 PM Result code OK UDS Response failed: [0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN]
I want to be able to search this file and extract all the data within the brackets and produce that as output like:
0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN 0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN
I tried this so far but no luck:
import re from pathlib import Path with open ("results.txt", "r") as myfile: data=myfile.readlines() ctr=0 for i in data: m = re.search(r"[([A-Za-z0-9_]+)]", str(i)) print(m)
Any input is greatly appreciated!
Advertisement
Answer
Try this:-
import re with open('results.txt') as infile: for line in infile: m = re.search(r'.*?[(.*)].*', line) if m: print(m.group(1))