python提取相同的字符串的指定数据
各位大佬好,python小白跪求各位python大佬。如下,我想提取两个Total:后面的数据,并加以区分,security对应1144 ,nat对应4 ,应该如何编写python脚本?HRP_M<HWFW66-OA01>display security-policy rule all
2022-11-11 19:25:19.907 +08:00
Total:1144
RULE IDRULE NAME STATE ACTION HITS
-------------------------------------------------------------------------------
1214 测试1111 enable permit 0
1199 访问测试123 enable permit 0
1187 临时专机测试 disable deny 0
1200 远程桌面 enable permit 0
1213 林开发远程桌面 enable permit 0
HRP_S<HWFW66-OA02>display nat-policy rule all
2022-11-11 19:25:41.527 +08:00
Total:4
RULE IDRULE NAME STATE ACTION HITS
-------------------------------------------------------------------------------
1 wlan_to_untrust enable src-nat 12063413
2 BG_to_wlan enable src-nat 5136278
3 WLAN_to_BG enable src-nat 0
0 default enable no-nat 2800986172
-------------------------------------------------------------------------------
本帖最后由 XyD3 于 2023-3-17 16:59 编辑
string = """
HRP_M<HWFW66-OA01>display security-policy rule all
2022-11-11 19:25:19.907 +08:00
Total:1144
RULE IDRULE NAME STATE ACTION HITS
-------------------------------------------------------------------------------
1214 测试1111 enable permit 0
1199 访问测试123 enable permit 0
1187 临时专机测试 disable deny 0
1200 远程桌面 enable permit 0
1213 林开发远程桌面 enable permit 0
HRP_S<HWFW66-OA02>display nat-policy rule all
2022-11-11 19:25:41.527 +08:00
Total:4
RULE IDRULE NAME STATE ACTION HITS
-------------------------------------------------------------------------------
1 wlan_to_untrust enable src-nat 12063413
2 BG_to_wlan enable src-nat 5136278
3 WLAN_to_BG enable src-nat 0
0 default enable no-nat 2800986172
-------------------------------------------------------------------------------
"""
import re
print({
k: v
for k, v in re.findall('[\s\S]*? (\S*?)-policy[\s\S]*?Total:(+)', string)
})
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\nTotal:(.*)\n"
test_str = ("display security-policy rule all\n"
"2022-11-11 19:25:19.907 +08:00\n"
"Total:1144\n"
"RULE IDRULE NAME STATE ACTION HITS \n"
"-------------------------------------------------------------------------------\n"
"1214 测试1111 enable permit 0 \n"
"1199 访问测试123 enable permit 0 \n"
"1187 临时专机测试 disable deny 0 \n"
"1200 远程桌面 enable permit 0 \n"
"1213 林开发远程桌面 enable permit 0 \n"
"HRP_S<HWFW66-OA02>display nat-policy rule all\n"
"2022-11-11 19:25:41.527 +08:00\n"
"Total:4\n"
"RULE IDRULE NAME STATE ACTION HITS \n"
"-------------------------------------------------------------------------------\n"
"1 wlan_to_untrust enable src-nat 12063413 \n"
"2 BG_to_wlan enable src-nat 5136278 \n"
"3 WLAN_to_BG enable src-nat 0 \n"
"0 default enable no-nat 2800986172\n"
"-------------------------------------------------------------------------------")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please vis 正则确实是最方便快捷的方案 可以使用正则表达式来提取总数:
import re
data = """
HRP_M<HWFW66-OA01>display security-policy rule all
2022-11-11 19:25:19.907 +08:00
Total:1144
RULE IDRULE NAME STATE ACTION HITS
1214 测试1111 enable permit 0
1199 访问测试123 enable permit 0
1187 临时专机测试 disable deny 0
1200 远程桌面 enable permit 0
1213 林开发远程桌面 enable permit 0
"""
security_total = re.search(r'Total:(\d+)', data).group(1)
print("Security Total:", security_total)
data = """
HRP_S<HWFW66-OA02>display nat-policy rule all
2022-11-11 19:25:41.527 +08:00
Total:4
RULE IDRULE NAME STATE ACTION HITS
1 wlan_to_untrust enable src-nat 12063413
2 BG_to_wlan enable src-nat 5136278
3 WLAN_to_BG enable src-nat 0
0 default enable no-nat 2800986172
"""
nat_total = re.search(r'Total:(\d+)', data).group(1)
print("NAT Total:", nat_total)
输出:
Security Total: 1144
NAT Total: 4 import re
text = """HRP_M<HWFW66-OA01>display security-policy rule all
2022-11-11 19:25:19.907 +08:00
Total:1144
RULE IDRULE NAME STATE ACTION HITS
-------------------------------------------------------------------------------
1214 测试1111 enable permit 0
1199 访问测试123 enable permit 0
1187 临时专机测试 disable deny 0
1200 远程桌面 enable permit 0
1213 林开发远程桌面 enable permit 0
HRP_S<HWFW66-OA02>display nat-policy rule all
2022-11-11 19:25:41.527 +08:00
Total:4
RULE IDRULE NAME STATE ACTION HITS
-------------------------------------------------------------------------------
1 wlan_to_untrust enable src-nat 12063413
2 BG_to_wlan enable src-nat 5136278
3 WLAN_to_BG enable src-nat 0
0 default enable no-nat 2800986172
-------------------------------------------------------------------------------
"""
s = dict(re.findall("display (.*)-policy.*\n.*\nTotal:(.*)\n", text))
print(s)
JJ0201Wang 发表于 2023-3-17 16:55
用正则就可以吧?先找所有的Total:(.*?)RULE,然后做if判断,如果是1144则输出security,否则输出nat
感谢大佬的解答,但是我这有很多个这种数据,每一个是不一样的 archer0258 发表于 2023-3-17 21:13
import re
text = """HRP_Mdisplay security-policy rule all
感谢大佬解答,我试试 kiki1226 发表于 2023-3-17 18:01
可以使用正则表达式来提取总数:
感谢大佬解答!{:301_993:}
页:
[1]