0

私はPythonが初めてです。

次のように、テーブル(およびその内容)を印刷する文字列があります

Name         At time Last  Inter\ Max    Logfile Location               Status  
                     time   val   log                                           
                           (mins) files                                         
------------ ------- ----- ------ ------ ------------------------------ ------- 
foo1           now   16:00   60   100    flash:/schedule/foo1/          Job     
                                                                        under   
                                                                        progress
foo2           now     -     60   100    -                              Waiting 
tech-support   now   16:00   60   100    flash:/schedule/tech-support/  Job     
                                                                        under   
                                                                        progress

文字列 " " がテーブルに何回Job under progress存在するかを調べる必要があります。試しlen( re.findall( pattern, string ) )てみlen( re.findall("(?=%s)" % pattern, string) )ましたが、どれも機能していないようです。

より良い提案はありますか?

4

1 に答える 1

3
data = """
Name         At time Last  Inter\ Max    Logfile Location               Status  
                     time   val   log                                           
                           (mins) files                                         
------------ ------- ----- ------ ------ ------------------------------ ------- 
foo1           now   16:00   60   100    flash:/schedule/foo1/          Job     
                                                                        under   
                                                                        progress
foo2           now     -     60   100    -                              Waiting 
tech-support   now   16:00   60   100    flash:/schedule/tech-support/  Job     
                                                                        under   
                                                                        progress
                                                                        """
import re
print len(re.findall("Job\s+under\s+progress", data))

出力

2

編集: 同じ行にある場合、正規表現はまったく必要ありません

data = """
Name         At time Last  Inter\ Max    Logfile Location               Status  
                     time   val   log                                           
                           (mins) files                                         
------------ ------- ----- ------ ------ ------------------------------ ------- 
foo1           now   16:00   60   100    flash:/schedule/foo1/          Job under progress
foo2           now     -     60   100    -                              Waiting 
tech-support   now   16:00   60   100    flash:/schedule/tech-support/  Job under progress
"""

print sum(1 for line in data.split("\n") if "Job under progress" in line)

出力

2
于 2013-10-28T06:21:13.390 に答える