2017-10-05 7 views
0

ファイル内にあるテキストブロックを見つける小さなプログラム/クラスを作成しようとしています。私はそのブロックから特定の情報を取り出し、それを単純化して印刷/返す。を使って繰り返し実行するPythonプログラム

この:これに

it /'properties'/'someJenkinsInformation'/'strategy(class:hudson.LogRotator)' 
{ 
'daysToKeep'('90') 
'numToKeep'('300') 
'artifactDaysToKeep'('3') 
'artifactNumToKeep'('3') 
} 

:私はこれまで持って何

logRotator(90, 300, 3, 3) 

# Search test.txt for 'LogRotator' 
def find_text(self): 
    super.find_text() 
    self.convert_to_string() 
    # now that we have found our line, find the next piece 


# From find_text print 'logRotator(90, 300, 3, 3)' 
def create_text(): 
    j = 0 

    while self.file_text[line_num + j].strip() != "}": 
     while self.file_text[line_num + j].strip() != ")": 
      match = re.search(r"[0-9]+", self.file_text[line_num + j]) 
    # this is mostly where I get lost in 
    # how to iterate through the above block and how to pull out what I 
    # need in order to print 
+0

スーパークラスとは何ですか? 'find_text()'は何を検索するのかを示す引数を取るべきではありませんか? – Barmar

+0

'line_num'とは何ですか? – Barmar

+0

各行の番号をリストに追加します。ループが終了すると、 '.join'を使ってリストを文字列" 90,300,3,3 "にすることができます。 – Barmar

答えて

1

私はあなたが求めているものについては完全に明確ではありませんよ。これはあなたが必要とする技術かもしれません。

これは、 '{'に遭遇したときに処理をオンにし、 '}'に一致するときに終了するという行単位の解析を含みます。

import re 

processing = False 
result = 'logRotator(' 
with open('temp.txt') as text: 
    for line in text: 
     if line.startswith('{'): 
      processing = True 
     elif line.startswith('}'): 
      break 
     else: 
      if processing: 
       m = re.search('\d+', line) 
       if m: 
        result += '%s, '%m.group() 
print (result[:-2]+')') 

次は出力です。

logRotator(90, 300, 3, 3) 
+0

ありがとうございました!私はほとんどそこにいて、これは大きな助けとなった。 –

+0

大歓迎です。それ以外の部分は幸運です。 –

関連する問題