如何使用python过滤文件中的井号?

rem*_*emy 0 python

文件内容如下:

##this is the comment

but this is not comment '####', LOL   # this is a normal comment
Run Code Online (Sandbox Code Playgroud)

我只是想过滤所有评论并获得过滤后的内容,但我不知道如何避免过滤不是评论的英镑符号.

fileLines = [line.strip() for line in file if '#' != line[0] ]
Run Code Online (Sandbox Code Playgroud)

这段代码只是可以过滤注释符号,这是一行的第一个字符.

我想要的结果就像下面一行:

but this is not comment '####', LOL
Run Code Online (Sandbox Code Playgroud)

Bur*_*lid 7

import shlex
filelines = [' '.join(shlex.split(line,True)) for line in file]
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅shlex文档.

  • @remy:你能得到多少简单?这是一个stdlib函数调用. (3认同)