1.1需求:读取一个100G的文件,检测文件中是否有关键字keys=['苍老师','小泽老师',"alex"],
如果有则替换成"***",并写入到另一个文件中
#19 需求:读取一个100G的文件,检测文件中是否有关键字keys=['苍老师','小泽老师',"alex"],
# 如果有则替换成"***",并写入到另一个文件中
f1=open("a1.txt",mode="r",encoding="utf-8")
f2=open("a2.txt",mode="w",encoding="utf-8")
keys=['苍老师','小泽老师',"alex"]
data=f1.readline()
while data:
for ele in keys:
if ele in data:
data=data.replace(ele,"***")
f2.write(data)
data=f1.readline()
f1.close()
f2.close()
#——————————————————————————————————————————————————————————————————————
#优化
keys=['苍老师','小泽老师',"alex"]
with open("a1.txt",mode="r",encoding="utf-8") as f1,open ("a2.txt",mode="w",encoding="utf-8")as f2:
data = f1.readline()
while data:
for ele in keys:
if ele in data:
data=data.replace(ele,"***")
f2.write(data)
data = f1.readline()
#——————————————————————————————————————————————————————————————————————————
#优化
keys=['苍老师','小泽老师',"alex"]
with open("a1.txt",mode="r",encoding="utf-8") as f1,open ("a2.txt",mode="w",encoding="utf-8")as f2:
for line in f2:
for ele in keys:
if ele not in line:
continue
else:
line=line.replace(ele,"***")
f.write(line)
2:车牌区域划分,请根据车牌信息,分析出各省的车牌持有数量
#20:车牌区域划分,请根据车牌信息,分析出各省的车牌持有数量
#优化:
cars=['鲁A32444','鲁B12333','京B8989M','黑c49678','黑c46555','沪B25041','黑c34567']
info={}
ul=[]
for title in cars:
title=title[:1]
if title in info:
pass
info[title]=0
for ele in cars:
ele=ele[:1]
if ele in info:
info[ele]+=1
print(info)
#————————————————————————————————————————————————————————————————
#优化一
for title in cars:
start=title[:1]
if start not in ele:
info[start]=1
else:
info[start]+=1
#————————————————————————————————————————————————————————————————
#优化二
for title in cars:
start=title[0]#截取第一个字
if start in info:
info[start]+=1
else:
info[start]=1
#——————————————————————————————————————————————————————————————————
#将大于44的数放到字典k1键中,小于k2的值放在k2中
info=[11,22,55,0,44,33,6,99]
dict1={}
for item in info:
if item > 44:
if 'k1' not in dict1:
dict1['k1']=[item]
else:
dict1['k1'].append(item)
else:
if 'k2' not in dict1:
dict1['k2']=[item]
else:
dict1['k2'].append(item)
3:读取文件内容,文件结构构造如下
#21:读取文件内容,文件结构构造如下
# id,name,age,phone,job
# 1,alex,22,136277444892,IT
# 2,wusir,23,133747474774,Teacher
# 3,taibai,18,1377747474,IT
#info=[{'id':"1","name":"alex","age":"22","phone":"136737899","Job":"IT"},{......}]
f1=open('data.txt',mode="r",encoding="utf-8")
title=f1.readline()
info=[]
title=title.strip().split(',')
data=f1.readline()
while data:
dict1={}
item=data.strip()
con=item.split(',')
for index in range(len(title)):
dict1[title[index]]=con[index]
info.append(dict1)
data = f1.readline()
f1.close()
print(info)
#——————————————————————————————————————————————————————————————————
4.九九乘法表
#22 九九乘法表
for num in range(1,10):
for num1 in range(1,num+1):
temp="%s*%s "%(num,num1)
print(temp,end='')
print()