我想寫一個小程序/類,我在文件中找到一定的文本塊。然後我從該塊中提取某些信息並簡化並打印/返回。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
什麼是超類?不應該'find_text()'採取一個參數告訴它要搜索什麼? – Barmar
'line_num'是什麼? – Barmar
將每行中的數字追加到列表中。當循環結束時,可以使用'.join'將列表變成字符串'「90,300,3,3」' – Barmar