#题目一
def modify_file(filename,old,new):
import os
with open(filename,'r',encoding='utf-8') as read_f,
open('.bak.swap','w',encoding='utf-8') as write_f:
for line in read_f:
if old in line:
line=line.replace(old,new)
write_f.write(line)
os.remove(filename)
os.rename('.bak.swap',filename)
modify_file('/Users/jieli/PycharmProjects/爬虫/a.txt','alex','SB')
#题目二
def check_str(msg):
res={
'num':0,
'string':0,
'space':0,
'other':0,
}
for s in msg:
if s.isdigit():
res['num']+=1
elif s.isalpha():
res['string']+=1
elif s.isspace():
res['space']+=1
else:
res['other']+=1
return res
res=check_str('hello name:aSB passowrd:alex3714')
print(res)
#题目三:略
#题目四
def func1(seq):
if len(seq) > 2:
seq=seq[0:2]
return seq
print(func1([1,2,3,4]))
#题目五
def func2(seq):
return seq[::2]
print(func2([1,2,3,4,5,6,7]))
#题目六
def func3(dic):
d={}
for k,v in dic.items():
if len(v) > 2:
d[k]=v[0:2]
return d
print(func3({'k1':'abcdef','k2':[1,2,3,4],'k3':('a','b','c')}))