python提取文件中自定义字符,并统计
dict1 = {'北京大学':0,'中国人民大学':0,'清华大学':0,'北京航空航天大学':0,'北京理工大学':0}
with open('gaoxiao.txt','r') as file:
    for i in file.readlines():
        for name in dict1.keys():
            if name in i:
                dict1[name] += 1
for i in dict1.keys():
    print(i,dict1[i])
txt文件:
 
 运行结果:
 










