首页 > 编程语言 > 详细

Python修改文件方法——字符串替换

时间:2018-04-28 23:15:33      阅读:220      评论:0      收藏:0      [点我收藏+]
#字符串替换
import sys
f = open("yesterday2","r",encoding="utf-8")
f_new = open("yesterday2.bak","w",encoding="utf-8")

find_str = sys.argv[1]
replace_str = sys.argv[2]
for line in f:
if find_str in line:
line = line.replace(find_str,replace_str)
f_new.write(line)
f.close()
f_new.close()

#with执行问了,文件就自动关闭了! 开发规范一行代码不能超过80字符: \
with open("yesterday2","r",encoding="utf-8") as f ,\   
open("yesterday2", "r", encoding="utf-8") as f2:
for line in f:
print(line)

Python修改文件方法——字符串替换

原文:https://www.cnblogs.com/wzsx/p/8969775.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!