如何从文本文件中删除评论过的块,评论会被#|
和|#\n
包围?
INFILE:
#|\n this is some sort of foo bar\n that I don't care about|#\nthen there is a foo bar sentence that I want but i don't want that foo bar in within the hex pipe pipe hex comment block.#| and even so, i don't want this section to appear|#\n with some crazy sentence...
期望的输出:
then there is a foo bar sentence that I want but i don't want that foo bar in within the hex pipe pipe hex comment block. with some crazy sentence...
是否有更好的方法可以删除除以下内容之外的注释块?
txt = '''#|\n this is some sort of foo bar\n that I don't care about|#\nthen there is a foo bar sentence that I want but i don't want that foo bar in within the hex pipe pipe hex comment block.#| and even so, i don't want this section to appear|#\n with some crazy sentence...'''
pointer = 0
while pointer < len(txt):
try:
start = txt.index('#|',pointer)
end = txt.index('|#\n',start)
cleantxt+=txt[pointer:start]
pointer = end+3
except ValueError:
cleantxt+=txt[pointer:]
break
答案 0 :(得分:1)
您可以使用regex:
>>> import re
>>> txt = '''#|\n this is some sort of foo bar\n that I don't care about|#\nthen there is a foo bar sentence that I want but i don't want that foo bar in within the hex pipe pipe hex comment block.#| and even so, i don't want this section to appear|#\n with some crazy sentence...'''
>>> txt2 = re.sub(r'#\|.*?\|#', '', txt, flags=re.DOTALL) # remove multiline comment
>>> txt2
"\nthen there is a foo bar sentence that I want but i don't want that foo bar in within the hex pipe pipe hex comment block.\n with some crazy sentence..."
您还可以strip()
删除不需要的换行符。{/ p>