我在附加现有词典的值时遇到问题。目标是打开一个 json 文件,分析现有的字典,查看是否存在任何服务,如果该服务存在,则附加新密码。
#!/usr/bin/env python3
import json
import random
#a set of characters to chose from for the passwords
char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()\{\}[]-_=+/?.>,<|`~'
services = []
passlength = 0
b = 0
#this function takes your input on what services you want in your list of dictionaries
def service_add():
serviceques = input('what service is the password for? ')
service = (serviceques)
if service == 'done':
starter()
elif service == '':
print('you must enter a service name')
service_add()
elif service == ' ':
print('you must enter a service name')
service_add()
else:
if service in services:
print('service and key already exists')
else:
services.append(service)
#print(services)
service_add()
#function to tell how long you want your password to be
def starter():
lengths = input('How long do you want the password to be? ')
global length
length = int(lengths)
makingPairs()
#this function creates a password and puts the password in a dictionary with each
#service in your list then appends the set of service and password to a json file
def makingPairs():
global b
global services
global length
a = 0
jsondics= []
for line in services:
a = a + 1
for x in range(a):
password = ''
for c in range(length):
password += random.choice(char)
jsonpairs = {
'Service' : services[b],
'Password' : password
}
这是来自 json 文件的原始数据
[{"Service": "spotify", "Password": "5QF50W,!UG"}, {"Service": "pandora", "Password": "E=b]|6]-HJ"}]
当我运行代码并尝试删除“潘多拉”时,它给了我这个例子
[{'Service': 'spotify', 'Password': 'bMXa2FY%Rh'}, {'Password': '$m--c<CY2x'}]
问题是,它没有删除整个字典,而是只删除了名为“Pandora”的键。我试图更改new_list变量,但它仍然只删除键或值,而不是整个变量。
炎炎设计
相关分类