I'm newbie to Python, I have coded to store the employee details in the dictionary format. I have clarification like, first of all I've initialized empty dictionary (user_details = {}) in the start of my code and then executed it and I have given all the values to the inputs,
Suppose If I re-run the code means I need to enter all the details again from start, because I've initialized a empty dictionary in the starting of my code. It resets and emptied all the existing details.
If I re-run the code means, I need to enter the values from the start. Is there any other way to store the existing details in the dictionary if I re-run the code also.
Please correct me, If I'm wrong.
Thanks for your time in advance !!
user_details = {}
while True:
user_input = input(" You're Operation Please ( New / View ) Details : ").lower()
if user_input == 'new':
create_user_ID = input(" Enter the user ID : ")
user_details[create_user_ID] = {}
user_name = input(" Enter the user name : ")
user_details[create_user_ID]['Name'] = user_name
user_age = int(input(" Enter the Age : "))
user_details[create_user_ID]['Age'] = user_age
user_occupation = input(" Enter the users occupation : ")
user_details[create_user_ID]['Occupation'] = user_occupation
user_department = input(" Enter the user department : ")
user_details[create_user_ID]['Department'] = user_department
user_income = int(input(" Enter the salary details : "))
user_details[create_user_ID]['Salary'] = user_income
user_address = input(" Enter the Address details ")
user_details[create_user_ID]['Address'] = user_address
print(f" New User account {create_user_ID} has been successfully created")
process = input(" Do you want to continue the Account creation process (YES / NO ) : ").lower()
if process == 'no':
break
elif user_input == 'view':
user_ID = input("Enter the user_ID : ")
print(user_details[user_ID])
break
else:
print(" Please enter the proper command to execute (new / view)")
for detail in user_details.items():
print(detail)
This should you get started:
d = {"1": 42, "None": "comfort"}
import csv
with open("f.txt", "w") as f:
writer = csv.writer(f)
writer.writerow(["key","value"])
# write every key/value pair as one csv-row
for key,value in d.items():
writer.writerow([key,value])
print(open("f.txt").read())
new_d = {}
with open("f.txt") as f:
reader = csv.reader(f)
next(reader) # skip header
# read every key/value pair from one csv-row, ignore empty lines
for line in reader:
if line:
key,value = line
new_d[key] = value
print(new_d)
Outputs:
# as file
key,value
1,42
None,comfort
# reloaded - all strings of course
{'1': '42', 'None': 'comfort'}
Also lookup the dict_writer / dict_reader from the csv module .
Using pickle,
In [11]: dict_to_store = dict(enumerate(['a']*10, 0))
In [12]: dict_to_store
Out[12]:
{0: 'a',
1: 'a',
2: 'a',
3: 'a',
4: 'a',
5: 'a',
6: 'a',
7: 'a',
8: 'a',
9: 'a'}
pickle is lot more easier when compared to other modules. Example,
import pickle
pickle.dump(dict_to_store, open('file_out.txt', 'w'))
In [13]: loaded_dict = pickle.load(open('file_out.txt', 'r'))
In [14]: loaded_dict
Out[14]:
{0: 'a',
1: 'a',
2: 'a',
3: 'a',
4: 'a',
5: 'a',
6: 'a',
7: 'a',
8: 'a',
9: 'a'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.