替换configparser 中指定的值

转自:https://blog.csdn.net/dingjha/article/details/88787557 
以读的方式打开,将文件读取到内存中
with open(r"./test.ini", "r", encoding="gbk") as f:
#存储为一个列表的形式
lines = f.readlines()
print("原文件为%s"%(lines))
# 写的方式打开文件
with open(r"./test.ini", "w", encoding="gbk") as f_w:
for line in lines:
if "token" in line:
# 替换
line = line.replace("token", "Iam token")
f_w.write(line)
posted @ 2019-11-04 15:39  yanhuidj  阅读(482)  评论(0编辑  收藏  举报