f=open("yesterday2",'r',encoding="utf-8")
f_new = open("yesterday2.bak","w",encoding="utf-8")
for line in f:
if "xxx" in line :
line = line.replace("xxx","xxx+1")
f_new.write(line)
import sys
find_str = sys.argv[1]
replace_str = sys.argv[2]
with open #(不需要关闭,自动关闭文件)
with open("yesterday2",'r',encoding="utf-8") as f:
for line in f:
print(line) #一行代码不能超过80个字符
作业:
实现简单的shell sed 替换功能
修改haproxy配置文件
字符编码与转换:
Ascii 1个字节
Unicode 中文两个字节
utf-8 #所有的英文是Ascii一个字节,中文还是Unicode三个字节
s_to_gbk.decode("gbk").encode("utf-8")
#-*-coding:gbk-*-
#-*-coding:gbk-*-
面向对象------》类-----》class
面向过程 -----》过程-----》def
函数式编程-----》函数----》def
import time
def logger():
time_format = '%Y-%m-%d %x'
time_current=time.strftime(time_format)
with open('a.txt,'a+') as f:
f.write('%s end action\n' %time_current)