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