我有一个注释文件的列表,其中我需要删除该文件中的注释行(#),并需要在同一个文件上写入。在
注释文件:#Hi this the comment
define host {
use template
host_name google_linux
address 192.168.0.12
}
#commented config
#ddefine host {
#d use template1
#d host_name fb_linux
#d address 192.168.0.13
#d}
我写的删除文件中注释行的代码?在
代码:
^{pr2}$
我尝试使用上述两种方法打印输出返回正确,但无法再次在同一文件上写入。在
在文件中输出:cat commentfile.txt
#Hi this the comment
define host {
use template
host_name google_linux
address 192.168.0.12
}
#commented config
#ddefine host {
#d use template1
#d host_name fb_linux
#d address 192.168.0.13
#d}
预期输出:cat commentfile.txt
define host {
use template
host_name google_linux
address 192.168.0.12
}
我甚至尝试过正则表达式方法,但无法在同一个文件上编写。在
RE方法:for line in file:
m = re.match(r'^([^#]*)#(.*)$', line)
if m:
continue
有什么建议吗?在