在现有字典中添加一个新键:值,然后使用泡菜保存该键,然后在另一个python文件中加载该字典

发布于 2025-02-06 04:20:49 字数 1013 浏览 4 评论 0原文

因此,基本上,我试图创建一个密码管理器,可以保存我的电子邮件和密码。 首先,我创建了一个字典,并使用泡菜:

import pickle

passDB = {} # this is the dictionary
key = input("Email: ") # taking email
password = input("Password: ") # taking password
passDB[key]= password # assigning new email and password into the dictionary
pickle.dump( passDB, open( "passDB.p", "wb" ) ) # saving it as passDB.p

将其保存为add2db.py。然后,我在Python文件中创建了一个密码管理器:

import pickle
passDB = pickle.load( open( "passDB.p", "rb"))
query1 = input("Search your email account to get password: ").lower()
if query1 in passDB.keys():
    print(passDB[query1])
else:
    print("Nothing such email in database")

将其保存为Passman.py。 因此,这里的问题是,假设我在add2db.py文件中添加了一个新的电子邮件和密码。它添加了完美。当我尝试通过Passman.py文件中的电子邮件搜索密码时,它会完美显示密码。但是同样,当我尝试在Add2db.py中添加另一个电子邮件和密码时,它再次添​​加了成功,但删除了我首先添加的现有电子邮件和密码。当我尝试使用passman.py查找时,没有第一个电子邮件和密码的迹象 谁能解决以下内容?

  1. 我想要一个创建add2db.py文件,该文件不断向现有词典添加新的电子邮件和密码。字典必须包含所有以前的电子邮件和密码。这样我就可以在passman.py中查找所有电子邮件和密码

So basically, I am trying to create a password manager where I can save my email and password.
first I created a dictionary and save it using pickle:

import pickle

passDB = {} # this is the dictionary
key = input("Email: ") # taking email
password = input("Password: ") # taking password
passDB[key]= password # assigning new email and password into the dictionary
pickle.dump( passDB, open( "passDB.p", "wb" ) ) # saving it as passDB.p

Saved it as add2db.py. Then I created a password manager in python file like this:

import pickle
passDB = pickle.load( open( "passDB.p", "rb"))
query1 = input("Search your email account to get password: ").lower()
if query1 in passDB.keys():
    print(passDB[query1])
else:
    print("Nothing such email in database")

Saved it as passman.py.
So, the problem here is, Suppose I added a new email and password into the add2db.py file. It adds perfectly. When I try to search password by email in passman.py file, it shows the password perfectly. But again, when I try to add another email and password in add2db.py, it again adds successfully but removes the existing email and password I added at first. And there is no sign of first email and password when I try to lookup using passman.py
Can anyone solve the following?

  1. I want a create add2db.py file that keeps adding new email and password to an existing dictionary. The dictionary must contain all previous email and passwords. So that I can look for all the email and password in passman.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2025-02-13 04:20:49

在add2db.py中,您需要加载数据库,添加新电子邮件,然后再次将其转储。用passdb = {}替换passdb = passdb = pickle.load(open(“ passdb.p”,“ rb”)))。希望这有帮助!

In add2db.py, you need to load your database, add the new email, and then dump it again. Replace passDB = {} with passDB = pickle.load( open( "passDB.p", "rb" ) ). Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文