我有一个属性文件,必须通过python编辑它。我需要编辑行jmx.admin.pwd=SomeRandomPassword
,并用我自己的密码替换随机密码。我不能这样做
文本文件如下所示:
some line
some line
some line
min.pop.password=SomeRandomNumbersWordsCharacters
some line
some line
some line
以下是修改后的输出:
some line
some line
some line
min.pop.password=My_Password
some line
some line
some line
任何帮助都非常感谢,因为我是Python新手
您可以做的是,首先打开文件,然后将所有行读入一个列表
content
,然后从每个行中删除\n
。从这里你可以在这个列表中搜索你的target
,其中包含单词或一些独特的短语,为此我们使用了password
。不,我们可以将它设置为target
,同时在=
处拆分它,还可以存储target_idx
。从这里我们只需改变target
的第二个索引,我们.split('=')
,然后.join()
把它们放在一起。现在我们可以将新行phrase
分配给content
的target_idx
来替换旧的target
。在打开text.txt
备份并使用'\n'.join(content)
编写新的content
之后之前
之后
相关问题 更多 >
编程相关推荐