Oct-12-2020, 09:04 PM
I am trying to create a binary file and save data to it but I get the following error message :
File was loaded in the wrong encoding: 'UTF-8'
I named the file employee.dat and I am using wb and we for reading and writing. Did I create the file wrong maybe? I don't think I have errors in my code. I right-clicked on my source folder and created a new file named employee.dat. Can you look at my code and see if I did do something wrong?
File was loaded in the wrong encoding: 'UTF-8'
I named the file employee.dat and I am using wb and we for reading and writing. Did I create the file wrong maybe? I don't think I have errors in my code. I right-clicked on my source folder and created a new file named employee.dat. Can you look at my code and see if I did do something wrong?
import Employee import pickle LOOK_UP = 1 ADD = 2 CHANGE = 3 DELETE = 4 QUIT = 5 FILENAME = 'employee.dat' def load_employees(): file = open(FILENAME, 'rb') eof_check = False while not eof_check: try: employee_dict = pickle.load(file) except IOError: employee_dict = {} except EOFError: eof_check = True file.close() return employee_dict def get_user_choice(): print('MENU') print('1. Look up an employee') print('2. Add a new employee') print('3. Change an existing employee') print('4. Delete an employee') print('5. Quit the program') print() try: choice = int(input('Enter your choice: ')) while choice < LOOK_UP or choice > QUIT: choice = int(input('Invalid. Enter your choice: ')) except: print('An error occurred ') return choice def look_up(employee_dict): id_num = input('Enter an employee ID number: ') # print(Employee.id_num) print(employee_dict.get(id_num, 'The specified ID number was not found.')) def add(employee_dict): name = input('Enter employee name: ') id_num = input('Enter employee id number: ') depart = input('Enter employee department: ') job = input('Enter employee job title: ') entry = Employee.Employee(name, id_num, depart, job) if id_num not in employee_dict: employee_dict[id_num] = entry print('The new employee has been added.') else: print('An employee with that ID already exists.') def change(employee_dict): id_num = input('Enter employee ID number: ') if id_num in employee_dict: name = input('Enter employee name: ') depart = input('Enter employee department: ') job = input('Enter employee job title: ') entry = Employee.Employee(name, depart, job) employee_dict[id_num] = entry print('Employee info has been changed') else: print('The specified ID number was not found') def delete(employee_dict): id_num = input('Enter an employee ID number:') if id_num in employee_dict: del employee_dict[id_num] print('Employee deleted') else: print('The specified ID number was not found') def save_employees(employee_dict): file = open(FILENAME, 'wb') pickle.dump(employee_dict, file) file.close() def main(): emp1 = Employee.Employee('Susan Meyers', 47899, 'Accounting', 'Vice President') emp2 = Employee.Employee('Mark Jones', 39119, 'IT', 'Programmer') emp3 = Employee.Employee('Joe Rodgers', 81774, 'Manufacturing', 'Engineer') my_dict = {47899: emp1, 39119: emp2, 81774: emp3} save_employees(my_dict) my_dict = load_employees() choice = 0 while choice != QUIT: choice = get_user_choice() if choice == LOOK_UP: look_up(my_dict) elif choice == ADD: add(my_dict) elif choice == CHANGE: change(my_dict) elif choice == DELETE: delete(my_dict) save_employees(my_dict) if __name__ == '__main__': main()