我想写一个小程序/类,我在文件中找到一定的文本块。然后我从该块中提取某些信息并简化并打印/返回。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