删除textfile-python中以“#|…”为边界的注释块

删除textfile-python中以“#|…”为边界的注释块,python,regex,string,comments,text-files,Python,Regex,String,Comments,Text Files,如何从文本文件中删除注释块,注释被封装在和中 infle: #|\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 se

如何从文本文件中删除注释块,注释被封装在

infle:

#|\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...
所需输出:

#|\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
txt=''.#| \n这是一种我不关心的foo-bar\n然后我想要一个foo-bar句子,但我不希望这个foo-bar出现在hex-pipe-pipe-hex注释块中。#| |即使如此,我也不希望这个部分出现|#\n带有一些疯狂的句子…''
指针=0
指针
您可以使用:

您还可以
strip()
删除结果以删除不需要的换行符。

您可以使用:

您还可以
strip()
删除不需要的换行符