2

I have a list that contains logs, and I am trying to add an empty line between specific blocks. So when I get to the end of a specific block, I can see a bit of separation, instead of looking like a continuous list.

EX:
log a:
kdsaldklsadkaslk
kasldkasldkasldkasldk

log a1:
lkpadkfaldkfdsl
klsdkfldskfsdl

So far I've tried all that I was able to find online, but I was unsuccessful. Either I am forced to add anything but an empty line (like a sequence of ----- for example), or the space added will be added to every single line (which is not what I want).

If I add in the list the empty line, like

log_list.append(" \n")

when I print the list using

print "\n".join(log_list)

all the empty lines that I have added are not printed.

But if I add any character to the append command, then it will be printed.

Is there any option that is automatically taking off the empty lines in a list, when I do the join command? Otherwise I do not understand why I cannot have an empty line in the list.

Is there another way to print out lists? I've always seen printing lists with the join command (all my objects in the list are strings).

Thanks!

4

2 に答える 2

2

\n私にとっては、これは機能します( a を追加してから aに参加するサルビジネスがないことを示しています\n):

>>> log_list = ['a']
>>> log_list.append('\n')
>>> log_list.append('b')
>>> log_list.append('c')
>>> print '\n'.join(log_list)
a


b
c

違うことをしたのは何ですか?

于 2013-01-15T00:42:11.593 に答える
1

リストがすでに作成されている場合は、たとえば、次の":"ように終わるすべての行の前に空白行を挿入できます

print "\n".join("\n"+s if s.endswith(":") else s for s in log_list)

ただし、ループを使用する方がおそらく明確です

for s in log_list:
    if s.endswith(":"):
        print
    print s

要件に合わせて条件を変更できます。s.startswith("log ")

于 2013-01-15T00:44:52.717 に答える