我正在制作一个使用 txt 文件保存用户名和密码的登录系统
我有两个读取文本文件和写入文本文件的函数。我还有另外两个函数可以将数据从字符串转换为字典并以格式返回:
词典:
details = {
username: password,
username: password,
username: password}
文本文件:
username password\n
username password\n
username password\n
每次运行 update_accounts() 时,字典的密码部分都会累积空格。我怀疑它使用了 format_accounts_upload()。这是我的程序:
...
def format_accounts_upload(accounts_dict):
"""Formats details dictionary into string ready for overwriting Menu Accounts.txt"""
accounts_string = ''
for username, password in accounts_dict.items():
#print(password)
accounts_string += username + ' ' + password + '\n'
return accounts_string
def update_accounts(new_details):
"""Writes the new account details to the txt file"""
new_details_string = format_accounts_upload(new_details)
account_write = open(path, 'w')
account_write.write(new_details_string)
account_write.close()
details = get_accounts()