python2.7中的字符串操作

2024-03-28 12:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个变量mystr(字符串类型) 其中包含以下数据:

zxoxcndsabcaa88912, (Used Disc Space,Requests per Minute,OS Memory Usage,Busy Threads,Disk I/O,Heap Memory Usage,CPU Load,Average Response Time,AccessTokenCount,RequestsCountPerMin,AverageResponseTimePerMin,CurrentBusyThreads,CurrentUsedMemory,ActiveClientRegistrationThreads,BorrowedConnectionsCount,ClientCount,RefreshTokenCount,FailedAccountTerminations,Error500CountMin,AuthorizationCodeCount)zxoxcndsabcaa900342, (Used Disc Space,Requests per Minute,OS Memory Usage,Busy Threads,Disk I/O,Heap Memory Usage,CPU Load,Average Response Time,AccessTokenCount,RequestsCountPerMin,AverageResponseTimePerMin,CurrentBusyThreads,CurrentUsedMemory,ActiveClientRegistrationThreads,BorrowedConnectionsCount,ClientCount,RefreshTokenCount,FailedAccountTerminations,Error500CountMin,AuthorizationCodeCount)

从这个我过滤如果繁忙的线程存在,那么它应该去系统退出,如果没有,那么它应该执行代码的其余部分。你知道吗

这是我的密码:

print type(mystr)
if 'Busy Threads' in mystr:
   print 'Busy threads is present'
   sys.exit(1)
else:
   print 'Busy threads not present'

检查()

但我在这里面临的问题是,除了忙线程之外,mystr的结果中还有一个字符串CurrentBusyThreads,因此即使忙线程不存在,也需要CurrentBusyThreads并执行不期望的其余部分。你知道吗

任何其他只过滤繁忙线程的方法


Tags: 字符串usagespacerequests线程useddiscprint
1条回答
网友
1楼 · 发布于 2024-03-28 12:27:06

使用regex来制定有关要查找的字符串的规则。你知道吗

你应该使用 检索(模式,字符串,标志=0)

扫描字符串,查找正则表达式模式生成匹配的第一个位置,并返回相应的MatchObject实例。如果字符串中没有与模式匹配的位置,则返回None;请注意,这与在字符串的某个点上查找零长度匹配不同。你知道吗

对于您的问题:

pattern = 'Busy\s+Threads'
if re.search(pattern, mystr):
    #do soemhting

链接到文档: https://docs.python.org/2/library/re.html

相关问题 更多 >