一、统计⽂件数据中出现的的所有字符与该字符出现的个数(不区分⼤⼩写,标点与空格也算)
⽂件内容: hello friend, can you speak English! # 结果: { 'h': 1, 'e': 4, 'l': 3, 'o': 2, ' ': 5, ... }
代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- #统计⽂件数据中出现的的所有字符与该字符出现的个数 #字符串方式 char_dict1 = {} with open('test.txt', 'r', encoding='utf-8')as f: f_str = f.read() for k1 in f_str: char_dict1[k1.lower()] = f_str.lower().count(k1.lower()) print(char_dict1) #seek方式 char_dict2 = {} with open('test.txt', 'r', encoding='utf-8')as f2: for k2 in f2.read(): f2.seek(0,0) char_dict2 [k2.lower()] = f2.read().lower().count(k2.lower()) print(char_dict2 ) #列表方式 char_dict3={} char_lsit=list() with open('test.txt', 'rt',encoding='utf-8')as f: for k5 in f.read(): char_lsit.append(k5.lower()) f.seek(0, 0) for k6 in f.read(): if k6 in char_lsit: char_dict3[k6]=char_lsit.count(k6) print(char_dict3)
二、 统计⽂件中⼤写字⺟、⼩写字⺟、数字及其他字符出现的次数
# ⽂件内容: Abc123,-+XYZopq000.?/ # 结果: { '⼤写字⺟': 4, '⼩写字⺟': 5, '数字': 6, '其他字符': 6 }
代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- #统计⽂件中⼤写字⺟、⼩写字⺟、数字及其他字符出现的次数 capital_count=0 lowercase_count=0 number_count=0 special_characters_count=0 with open('test2.txt','rt',encoding='utf-8') as f: string=f.read() for i in string: if (ord(i) >= 33 and (ord(i)<= 47)) or ((ord(i) >= 58 and (ord(i)<= 64) )): special_characters_count += 1 elif ord(i) >= 48 and (ord(i)<= 57): number_count += 1 elif ord(i) >= 65 and (ord(i)<= 90): capital_count += 1 elif ord(i) >= 97 and (ord(i)<= 122): lowercase_count += 1 string_count = {'大写字母':capital_count, '小写字母':lowercase_count, '数字':number_count, '特殊字符':special_characters_count} print(string_count)
三、登录注册系统
# 需求分析: 1.可以循环登录注册,输⼊1代表选择登录功能,输⼊2代表注册功能,输⼊0代表退出,其他输⼊代表 输⼊有误,重输 2.⽤户的账号密码信息存放在usr.txt⽂件中,保证⽤户注册成功后,重启系统,⽤户信息仍然保存 3.登录在账号验证通过才输⼊密码验证登录,账号验证三次失败⾃动进⼊注册功能,登录三次验证失败 ⾃动退出系统 4.第⼀次注册,⽂件写⼊ 账号:密码 信息,再次注册追加写⼊ |账号:密码 信息
代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- import time white_user_set=[] black_user_set=[] user_list=[] white_userDB='userDB' black_userDB='user_blackDB' with open(black_userDB, 'r+',encoding='utf-8') as black_f: for black_user in black_f.readlines(): black_user = black_user.strip(" ") black_user_set.append(black_user) with open(white_userDB, 'r+') as white_f: for file in white_f.readlines(): file = file.strip(" ") user_field,passwd_field,remarks_field= file.split('|') name1_field, user=user_field.split(':') name2_field, passwd = passwd_field.split(':') name3_field, remarks = remarks_field.split(':') white_user_set.append({name1_field:user,name2_field:passwd,name3_field:remarks}) user_list.append(user) print(white_user_set) def mod_info(file_name,old_str,new_str): mod_date='' with open(file_name,'r+',encoding='utf-8') as f: for line in f: line = line.replace(old_str,new_str) mod_date += line with open(file_name, 'w', encoding='utf-8') as f1: f1.writelines(mod_date) #用户注册函数 def user_register(): count=0 while True: count+=1 register_name = input('创建用户名:') register_passwd = input('创建密码:') register_passwd1 = input('创建密码:') user_remarks = input('输入您的备注信息,默认为空:') if user_remarks == '': user_remarks = 'null' if register_passwd == register_passwd1: if register_name in user_list: print('用户已存在,请重新创建') else: user_information=register_name+':'+register_passwd+':'+user_remarks print(user_information) print(type(user_information)) with open(white_userDB,'a+') as f: f.write(' ') f.write(user_information) print('恭喜您,用户创建成功!!!') return 0 else: if count == 3: print('失败次数超过上限,程序退出') exit(3) print('两次密码不一致,请重新创建2')
#用户登录函数 def user_login(): Flag = True user_failures = 0 all_failures = 0 while Flag: login_name = input('请输入用户名:') login_passwd = input('输入密码:') print(login_name,login_passwd) if login_name in black_user_set: print('该用户已被锁定,请联系您的管理员!') exit(3) if login_name in user_list: for subscript,line in enumerate(white_user_set): if (login_name == line['name']) and (login_passwd == line['passwd']): print('登录成功') print('正在登录。请稍后') time.sleep(1) print('') print('='*30) print('欢迎来到 250 世界') print('='*30) Flag = False break for subscript,line in enumerate(white_user_set): if (login_name == line['name']) and (login_passwd != line['passwd']): user_failures += 1 all_failures += 1 if (user_failures == 3): print('验证失败次数过多,用户被锁定') with open(black_userDB,'a+') as f: f.write(login_name+' ') exit(2) print('用户名或密码不正确,登录失败,请重新登录 2 ') break else: print('用户名或密码不正确,登录失败,请重新登录 3 ') all_failures += 1 if all_failures == 6: print('失败次数过多,请确认后再登录 4') exit(1) #用户信息修改函数 def user_info_mod(user_name,user_passwd,remarks): user_login print(''' 1.修改密码 2.修改备注信息 ''') info=input('请根据提示,选择您要修改的用户信息') if info == '1': confirm_password = input('请输入您当前的密码:') new_password = input('请输入您要修改的密码:') if confirm_password == user_passwd: print('您输入的当前密码有误,请程序输入!') else: for subscript, line in enumerate(white_user_set): if (user_name == line['name']): line[passwd]=new_password mod_info(white_userDB, confirm_password, new_password ) elif info == 2: new_remarks=input('请输入您要修改的备注:') for subscript, line in enumerate(white_user_set): if (user_name == line['name']): line[remarks]=new_remarks mod_info(white_userDB, line[remarks], new_remarks) else: print('无效的输入,请重新输入!') #主函数 def main(): while True: count=0 print('1 注册用户:') print('2 登录系统:') print('3 修改备注信息:') user_operation=input('请选择您的操作:') print(type(user_operation)) if user_operation == '1' or user_operation == '3': user_register() break elif user_operation == '2': user_login() break else: count+=1 if count == 3: print('失败次数过多,请确认后再登录') exit(3) print('非法的输入,请重试') if __name__ == '__main__': main()