I am coding in python and I'm using json to save the data however I always got an error in json file. I am using a nested dict. It's appending but creating a new nested dict, not into the original nested dict
My target output is
{
"1234" : {
Name: "wkwkw"
Age: "15"
}
"54321" : {
Name: "wkwkw"
Age: "15"
}
}
but I keep getting
{
"1234" : {
Name: "wkwkw"
Age: "15"
}
}
{
"54321" : {
Name: "wkwkw"
Age: "15"
}
}
which always error
this is my code
def register(self):
self.emp_id = input("Employee ID: ")
self.password = input("Password: ")
employee_record = {}
employee_details = {self.emp_id : employee_record}
employee_record["Employee ID"] = self.emp_id
employee_record["Password"] = self.password
employee_record["Job"] = self.job
employee_record["Name"] = self.name
employee_record["Age"] = self.age
employee_record["Gender"] = self.gender
employee_record["Contact Number"] = self.contact_no
print("Registration Successful!")
#print(employee_record)
with open('emp_details.json', 'w') as fp:
json.dump(employee_details, fp, indent=4)
fp.close()
def login(self):
file = open("emp_details.json", 'r')
emp_login_details = json.load(file)
print (emp_login_details)
self.emp_id = input("Enter login ID: ")
self.password = input("Enter your password: ")
if self.emp_id in emp_login_details[self.emp_id]["Employee ID"]:
if self.password in emp_login_details[self.emp_id]["Password"]:
print("You are now logged in!")
else:
print("You are not logged in!")
file.close()
w
as file opening option, I am wondering if not all content is deleted and replaced by only the latest entry ...