python取消注释右行
我有一个文件如下:
line 1: _____
...
# for AAA
#export CONFIG = AAA_defconfig
# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig
# for CCC, CcC, Ccc, Ccc1
#export CONFIG = CCC_defconfig
...
other lines
我想操作该文件,以便根据给定的字符串,我可以导出正确的“CONFIG”,并评论其他文件。 例如,如果我得到“CcC”,那么该文件将被操纵,
line 1: _____
...
# for AAA
#export CONFIG = AAA_defconfig
# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig
# for CCC, CcC, Ccc, Ccc1
export CONFIG = CCC_defconfig
...
other lines
在 python 中执行此操作的好方法是什么?
提前致谢!!
I have a file as follows:
line 1: _____
...
# for AAA
#export CONFIG = AAA_defconfig
# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig
# for CCC, CcC, Ccc, Ccc1
#export CONFIG = CCC_defconfig
...
other lines
I want manipulate the file, so that based on the given string, I could export the right "CONFIG", and comment the others.
e.g. if I got "CcC", then the file would be manipulated as
line 1: _____
...
# for AAA
#export CONFIG = AAA_defconfig
# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig
# for CCC, CcC, Ccc, Ccc1
export CONFIG = CCC_defconfig
...
other lines
what the good way to do it in python?
Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么为什么不将其设置为
除非这不是一个 python 文件并且您想要操作它。看起来是这样的。
在这种情况下,创建一个配置生成器,它将根据行变量创建一个配置文件。
[编辑:基于评论]
您可能需要进行一些调整,但这应该有效。
输入:文件1
输出:文件2
Then why not make it as
unless this is not a python file and you want to manipulate it. It looks like that.
In that case create a config generator which will create a config file based on the line variable.
[Edit: Based on comments]
You might have to make some adjustments but this should work.
Input: file1
Output: file2
IMO 更干净、更易读的方法。
(是的,要修改文件中的一行,您必须覆盖并重写整个文件。)
A little cleaner more readable approach IMO.
(And, yes, to modify one line in a file, you have to overwrite and rewrite the entire file.)
首先创建一个生成器函数:
现在测试它:
现在使用它来修复您的文件:
First create a generator function:
Now test it:
Now use it to fix your file: