Код: Выделить всё
class Employee:
def __init__(self, id, name, salary):
self.id = id
self.name = name
self.salary = salary
def display(self):
print('{:5d} -- {:20s} -- {:10.2f}'.format(self.id, self.name, self.salary))
Код: Выделить всё
import pickle
file = open('employee-data.csv', 'wb')
n = int(input('How many employees ?'))
for i in range(n):
id = int(input('Enter the Employee id:'))
name = input('Enter the Employee name:')
salary = float(input('Enter the Employee salary:'))
ob = Employee(id, name, salary)
pickle.dump(ob,file)
file.close()
import pickle
file2 = open('employee-data.csv', 'rb')
print('Employee Details ....')
while True:
try:
obj = pickle.load(file2)
obj.display()
except EOFError:
print('End of File Reached ...')
break
file2.close()
< /code>
ошибка: < /p>
> ---------
UnpicklingError Traceback (most recent call
> last) Cell In[5], line 9
> 7 while True:
> 8 try:
> ----> 9 obj = pickle.load(file2)
> 10 obj.display()
> 11 except EOFError:
>
> UnpicklingError: invalid load key, '\xef'.
< /code>
Следующий снимок показывает, что код для сериализации объекта успешно работает. И файл был создан.
Что может быть проблемой здесь? Любые предложения/отзывы оцениваются.
Подробнее здесь: https://stackoverflow.com/questions/797 ... ad-key-xef