删除文本文件中的“#| ... |#”限制的注释块 - python

时间:2013-08-13 10:08:15

标签: python regex string comments text-files

如何从文本文件中删除评论过的块,评论会被#||#\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

1 个答案:

答案 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>