1. How to read and manipulate the password of the user name stored in the file as a string
eg:mling,123456
niuniu,234567
luoluo,345678
Method One: Convert the string to a dictionary
1) Dictionary search efficiency is the highest, first define a dictionary, store user name and password
2) Open the file containing the user name and password, read the file operation
3) Loop the contents of the file, the content of the loop is a string line for a string such as: ' mling,123456 ', split the content: ' mling ' 123456 '
4) separate the separated strings as strings, and place them in the defined dictionary as key and value, respectively.
5) After the user name and password in the file, in the form of key and value, the dictionary is used to determine the contents of the input and the file, it can be manipulated in a dictionary.
all_users = {} #用来存放所有的用户名和密码
with open (' Users.txt ') as fr:
for line in fr:
up = Line.strip (). SPL It (', ') #分隔账号密码
# print (' account password after separation ', up)
S_username = up[0] #账号
s_pwd = up[1] #密码
all_users[s_username]=s_pwd# the account as key, and the password as value in the user's Dictionary
# Print (all_users)
Print (' Welcome to the Aries Product Management System '). Center (50 , ' * '))
Username = input (' Input account: '). Strip ()
pwd = input (' Enter password: '). Strip ()
if username in all_users:
If All_use Rs[username]==pwd: #登录成功
Login_tag = True
Else:
print (' Wrong password! ')
Else:
Print (' User not present ')
method Two: convert string to list
1) Define two lists to hold the user name and password
2) Loop the file, split the content, and store it in the list of user names and passwords, respectively
3) Because of the existence of the user name and password subscript is corresponding, you can use subscript to determine the consistency of the user name and password
f = open (' User.txt ', ' A + ')
F.seek (0) #把文件指针移动到最前面
Users = [] #这个存所有的用户名
Pwd=[] #这个存所有的密码
For line in F:
Username = Line.split (', ') [0]
passwd = Line.split (', ') [1].strip ()
Users.append (username)
Pwd.append (passwd)
Print (' Welcome to Product Handling Program '. Center (50, ' * '))
Username = input (' Please enter user name: '). Strip ()
passwd = input (' Please enter password: '). Strip ()
If username in users and passwd ==pwd[users.index (username)]: #判断用户名是否存在
Print (' Congratulations on your landing success ')
Else
Print (' The user entered does not exist! ‘)
2. How to read and manipulate the password of the user name stored in the file as a dictionary
The contents of the file exist in the following format:
{' Luoluo ': {' money ': "5435, ' passwd ': 234567}, ' admin ': {' money ': ' 12.12 ', ' passwd ': ' 123456 '}}
1) Read the contents of the file and convert the contents of the file into a dictionary format.
2) Determine whether the user name entered is in the dictionary, if so, determine if the input is not the corresponding password
With open (' Users.txt ') as fr:
Fr.seek (0)
User_str = Fr.read ()
User_dic = eval (user_str) #将字符串转换为字典
If username in User_dic:
if pwd = = str (user_dic[username][' passwd ')):
Print ("Congratulations on your successful landing!") ")
Log ("Congratulations on%s landing success!") "%username)
Return True
Else
Print ("The password entered is incorrect!") ")
Return False
Else
Print ("The user name you entered does not exist")
Return False
3. Add, delete, modify, and query the contents of the file.
1) Add user information in the following format: Note that more than one time, you need to add it in the following format
User_dic[user_name] = {' passwd ': user_pwd, ' money ': User_money}
2) Delete user Information format as follows: User_dic.pop (Del_name)
3) Modify the user information format as follows: Note the format and add the user is the same, after the key exists, can be modified, does not exist, can be added
User_dic[upadte_name] = {' passwd ': new_passwd, ' money ': New_money}
4) Query the user Information format as follows: Directly based on key query
Def UM (): # 4 User Management
If username = = ' admin ' and pwd = = ' 123456 ':
Userm = input ("You can do the following for users: 1 Add users, 2 delete users, 3 modify user information, 4 query users:")
Fu = open (' Users.txt ', ' A + ')
Fu.seek (0)
User_str = Fu.read ()
User_dic = eval (user_str)
if userm = = ' 1 ': # Add user Information
user_name = input (' Please enter the added user name: '). Strip ()
User_pwd = input (' Please enter the password added: '). Strip ()
User_money = input (' Please enter the amount added: '). Strip ()
If user_name! = "and user_pwd! =" and User_money! = ":
If user_name in User_dic:
Print (' User name already exists! ‘)
Elif Is_float (User_money) is not True:
Print (' The amount you entered does not match the decimal rule ')
Else
User_dic[user_name] = {' passwd ': user_pwd, ' money ': User_money}
Fu.seek (0)
Fu.truncate ()
Fu.write (str (user_dic))
Print (' user added success ')
Log ("User%s added successfully!") "% user_name)
Else
Print (' User name, password, amount cannot be null ')
if userm = = ' 2 ': # Delete User Information
Del_name = input (' Please enter the user name you want to delete: '). Strip ()
If Del_name in User_dic:
User_dic.pop (Del_name)
Fu.seek (0)
Fu.truncate ()
Fu.write (str (user_dic))
Print (' Delete user success ')
Log ("User%s deleted successfully!") "% del_name)
Else
Print (' The user you deleted does not exist ')
if userm = = ' 3 ': # Modify user Information
Upadte_name = input (' Please enter the user name you want to modify: '). Strip ()
If Upadte_name in User_dic:
NEW_PASSWD = input (' Change password to: ')
New_money = input (' Modified amount: ')
User_dic[upadte_name] = {' passwd ': new_passwd, ' money ': New_money}
Fu.seek (0)
Fu.truncate ()
Fu.write (str (user_dic))
Print (' Modify user success ')
Log ("User%s modified successfully!") "% upadte_name)
Else
Print (' The user you modified does not exist ')
if userm = = ' 4 ': # Query user Information
Check_name = input (' Please enter the name of the user you want to query: '). Strip ()
If Check_name in User_dic:
C_PW = user_dic[check_name][' passwd ']
C_money = user_dic[check_name][' money ']
Log ("User%s query succeeded!") "% check_name)
Print (' User name is: '%s ', user password is '%s ', user balance is '%s ' '% (Check_name, C_PW, C_money))
Else
Print ("The user name you queried does not exist!") ")
Return True
Else
Print (' Please enter the 1-4 option! ‘)
Return False
Else
Print ("The user name you entered does not have permission")
Return False
Processing of files in Python