1、写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作
import os
def change_file(file_name,old,new):
with open(file_name,'r',encoding='utf-8') as f,
open('{}.swap'.format(file_name),'w',encoding='utf-8') as w_f:
for line in f:
if old in line:
line = line.replace(old,new)
w_f.write(line)
os.remove(file_name)
os.rename('{}.swap'.format(file_name),file_name)
change_file('kkk.txt','123','456')
2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
def check_num(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_num('the name is hahaha 3k536:!#$')
print(res)
3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
def len_num(msg):
if len(msg) > 5:
print('长度超过5')
else:
print('长度小于5')
len_num('namege')
len_num([1,3,4,6,7])
len_num((2,4,5,6,7))
4、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
def list_len(msg):
if len(msg) > 2:
return msg[:2]
msg = list_len([1,2,3,4,5])
print(msg)
5、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
def list_content(msg):
return msg[::2]
msg = list_content([1,2,3,4,5,6,7,8,9])
print(msg)
6、写函数,检查字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
PS:字典中的value只能是字符串或列表
def check_len(dic):
d = {}
for k,v in dic.items():
if len(v) > 2:
d[k] = v[0:2]
return d
d = check_len({"k1": "v1v1", "k2": [11,22,33,44]})
print(d)