全部打印在

2024-09-25 16:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

下面我提供的代码一次打印一行输出。但是,我想重写代码,一次打印所有内容。你知道吗

def filters():
            for LogLine in Log:
                flag = True
                for key,ConfLine in Conf.items():
                    for patterns in ConfLine:
                        print patterns
                        if re.match((DateString + patterns), LogLine):
                            flag = False
                            break 

                    if(flag == False):
                        break 

                if(flag):

                print LogLine

谢谢


Tags: 代码inlogfalse内容forifdef
2条回答

以下是一般技巧:

lines = []
for ...
    lines.append(<whatever you were going to print>)
print '\n'.join(lines)

有一件事我会做。我将初始化一个空字典或空列表,然后将所有项附加到空字典或空列表中。最后一次打印输出。你知道吗

def filters():
    mypatterns=[]
    for LogLine in Log:
        flag = True
        for key,ConfLine in Conf.items():
            for patterns in ConfLine:
                print patterns
                mypatterns.append(patterns)
                if re.match((DateString + patterns), LogLine):
                    flag = False
                    break
            if(flag == False):
                break
        if(flag):
            print LogLine

print mypatterns

相关问题 更多 >