为 Python 中的所有事件提取文件中括号之间的字符串
Extract String between brackets in a file for all occurrences in Python
我有一个这样的文件:
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]
我希望能够搜索此文件并提取括号内的所有数据并将其生成为如下输出:
0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN
0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN
到目前为止我试过了,但没有成功:
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)
非常感谢任何意见!
你已经有了答案
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))
if m:
# print only the first match
print(m[0][1:-1])
print(m.groups[0])
help(m.groups)
"""
Help on built-in function groups:
groups(default=None) method of re.Match instance
Return a tuple containing all the subgroups of the match, from 1.
default
Is used for groups that did not participate in the match.
"""
试试这个:-
import re
with open('results.txt') as infile:
for line in infile:
m = re.search(r'.*?\[(.*)].*', line)
if m:
print(m.group(1))
我有一个这样的文件:
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]
我希望能够搜索此文件并提取括号内的所有数据并将其生成为如下输出:
0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN
0000NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0DNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNN0NN00N00NN0NNNNNNNNNNNNNN
到目前为止我试过了,但没有成功:
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)
非常感谢任何意见!
你已经有了答案
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))
if m:
# print only the first match
print(m[0][1:-1])
print(m.groups[0])
help(m.groups)
"""
Help on built-in function groups:
groups(default=None) method of re.Match instance
Return a tuple containing all the subgroups of the match, from 1.
default
Is used for groups that did not participate in the match.
"""
试试这个:-
import re
with open('results.txt') as infile:
for line in infile:
m = re.search(r'.*?\[(.*)].*', line)
if m:
print(m.group(1))