簡體   English   中英

Python在列表中查找字符串

[英]Python find string in list

我有一個salt模塊,它返回以下輸出(即將所有這些存儲到列表中以使迭代更容易):

    - 2015-05-21 19:25:08,060 [main] WARN  [::::::] c.p.core.FilteringPropertyPlaceholderConfigurer - Could not load properties from class path resource [proferi-component-test.properties]: class path resource [proferi-component-test.properties] cannot be opened because it does not exist
    - 2015-05-21 19:25:08,064 [main] WARN  [::::::] c.p.core.FilteringPropertyPlaceholderConfigurer - Could not load properties from class path resource [qe-prop-not-specified]: class path resource [qe-prop-not-specified] cannot be opened because it does not exist
    - 2015-05-21 19:25:13,290 [main] INFO  [::::::] c.p.a.m.persistence.modular.ModelSessionManager - Setup SessionManager modelSessionFactory
    - 2015-05-21 19:25:14,327 [main] INFO  [::::::] c.p.a.model.persistence.BlueprintsGraphReadSession - Loading model graph for application M-00000304-0000-0001-0000-000000000000 with version MV-0000000000002714-0000000000002695-true
    - 2015-05-21 19:25:14,658 [main] INFO  [::::::] c.p.a.m.p.hydration.AppModelGraphHydrator - AppModelGraph Hydration stats for app M-00000304-0000-0001-0000-000000000000 - total time:322ms | sql time:20ms | jackson mapping:32ms | vertex adding:6ms | core building:63ms | core population:15ms | proxying:84ms | invocation handler creation:80ms | interface list building:10ms | moving through result set:4ms | items processed:156
    - 2015-05-21 19:25:14,860 [main] INFO  [-:sales02:Session:SetPasswd:-:-:-] c.l.n.cluster.zookeeper.ZooKeeperClusterClient - Starting ClusterClient...
    - 2015-05-21 19:25:14,914 [main] INFO  [-:sales02:Session:SetPasswd:-:-:-] c.l.n.cluster.zookeeper.ZooKeeperClusterClient - Cluster started
    - 2015-05-21 19:25:14,915 [main] INFO  [-:sales02:Session:SetPasswd:-:-:-] com.proferi.core.NorbertProtobufServiceClient - Constructing NettyNetworkClient with close channel time -1 ms, max cnxns per node 10, stale request timeout 20 minutes, stale request purge frequency 2 minutes
    - 2015-05-21 19:25:14,961 [Thread-8] INFO  [-:sales02:Session:SetPasswd:-:-:-] c.l.n.c.zookeeper.ZooKeeperClusterManagerComponent - Connected to ZooKeeper
    - 2015-05-21 19:25:14,987 [Thread-8] INFO  [-:sales02:Session:SetPasswd:-:-:-] c.l.n.c.zookeeper.ZooKeeperClusterManagerComponent - Handling a Connected message
    - 2015-05-21 19:25:15,245 [main] INFO  [-:sales02:Session:SetPasswd:-:-:-] com.company.platform.cli.SetPassword - Password for email address john@tech.com for tenant sales02 was set
    - 2015-05-21 19:25:15,254 [main] INFO  [-:-:-:-:-:-:-] c.l.norbert.network.netty.NettyNetworkClient - Shutting down NetworkClient
    - 2015-05-21 19:25:15,273 [main] INFO  [-:-:-:-:-:-:-] c.l.norbert.network.netty.NettyNetworkClient - NetworkClient shut down
    - 2015-05-21 19:25:15,281 [main] INFO  [-:-:-:-:-:-:-] c.l.n.cluster.zookeeper.ZooKeeperClusterClient - Cluster shut down

從此返回我想檢查塊是否具有字符串

- 2015-05-21 19:05:18,108 [main] INFO  [-:sales02:Session:SetPasswd:-:-:-] com.company.platform.cli.SetPassword - Password for email address john@tech.com for tenant sales02 was set

做這個的最好方式是什么?

我嘗試使用for循環-

for i in l:
    if s == i: # where s is the string above
        return True
    else:
        return False

但是這很簡單(返回False)。 我嘗試使用正則表達式,但是該字符串太復雜,無法提供正則表達式。 任何幫助將不勝感激。

由於這是一個列表,我知道我可以使用索引來獲取所需的字符串,但是我不想要該字符串,因此我想在那里檢查它,然后將自定義字符串返回給用戶。

一個更簡單的方法是使用類似:

if s in l:
    return True

不需要像這樣簡單的檢查就可以進行for循環,它將遍歷列表並在命中匹配項時返回正數(或您想返回的任何值)。 這也是在python腳本的CLI級別菜單中建立一些魯棒性的一種方法。

提示:您可能需要將要檢查的字符串列表更改為一組。 運行速度稍快。

編輯:更好的方法:

return s in l

如果您不願意使用for循環-

for i in l:
   if any(i == s for i in entry)
        return True

盡管這可能是一個不太優雅的解決方案,但是使用any()確實可以使您的匹配比上面的簡單理解更柔和-但這是有風險的,因為如果列表中的字符串中至少包含您的匹配條件字符串以及更多信息,它也會返回true。

如果您想真正使用正則表達式,可以嘗試以下方法。 它可能不像簡單地遍歷列表那樣有效,但是它可以使您了解如何實現它。

盡管正則表達式的字面匹配很復雜(而且很雜亂),但它允許您通過匹配組獲取日志的每個組成部分。 在真實的環境中,您需要查看類似Logstash的東西,該東西利用了Grok過濾器(學習它們,它們很有趣!)。

注意:以下正則表達式並非100%准確,並且可能需要更改以獲取更多數據,但是您可以理解。

所需行的文字匹配如下所示:

-\s(\d{4}-\d{1,2}-\d{1,2})\s(\d{1,2}:\d{1,2}:\d{1,2},\d{1,3})\s(\[\w+\])\s(\w+)\s+(\[.*\])\s([a-zA-Z.]+)\s-\s(.*)

要僅使消息成為匹配組:

(?:.*)\s(([a-zA-Z_$][a-zA-Z\d_$]*\.)*([a-zA-Z_$][a-zA-Z\d_$]+)\s-\s)(.*)

例如,我不會使用列表,而是使用一個字符串來演示:

import re

logLine = "- 2015-05-21 19:05:18,108 [main] INFO  [-:sales02:Session:SetPasswd:-:-:-] com.company.platform.cli.SetPassword - Password for email address john@tech.com for tenant sales02 was set"
rx = "-\s(\d{4}-\d{1,2}-\d{1,2})\s(\d{1,2}:\d{1,2}:\d{1,2},\d{1,3})\s(\[\w+\])\s(\w+)\s+(\[.*\])\s([a-zA-Z.]+)\s-\s(.*)"

reMatch = re.match(rx, logLine)

if (reMatch):
    print reMatch.group(1)
    # Groups 1-7

您可以遍歷匹配組,並且會看到類似的日志行將為您提供:

  1. 2015-05-21
  2. 19:05:18,108
  3. [main]
  4. INFO
  5. [-:sales02:Session:SetPasswd:-:-:-]
  6. com.company.platform.cli.SetPassword
  7. Password for email address john@tech.com for tenant sales02 was set

因此,您可以檢查匹配組7以查看消息中所需的字符串是否存在,或者簡單地與第二個正則表達式交替以僅檢查一個組:

reMatch2 = re.match(rx2, logLine)

if (reMatch2):
    print reMatch2.group(1)

使用縮短的正則表達式,您將得到類似的結果,但它可能使您將來可以使用日志做更多的事情,而不僅僅是簡單地確定行中是否存在某些內容(或行本身)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM