write and Replace particular line in file

后端 未结 4 1061
刺人心
刺人心 2021-01-19 18:55

I want to replace value of key(i.e db_host, addons_path) with $$$$.

Input text file contains the following:

#         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-19 19:36

    What about something like this ?

    def replace_string(s):
        s = s.replace('db_host','$$$$')
        return s
    with open('test.txt','r') as fo:
        line = fo.read() # you could use strip() or split() additionally
    new_string = replace_string(line)
    

提交回复
热议问题