我正在研究一個簡單的員工系統,用於在Python3中學習面向對象的編程。 我的腳本正常工作,不包括保存和加載員工字典。 問題是我的字典不是這個代碼的正常字典原因: Employees[eid] = Employee(eName,eSalary,eAge)
我想使這個數據庫JSON可序列化,但我不知道,我也沒有想到,我在互聯網上發現它。Python3詞典風格對象映射到JSON可序列化
可悲的是在堆棧溢出將系統代碼給我的癌症,所以我貼我的代碼要點: https://gist.github.com/ShockvaWe/d82d89f767506c1ff682a4cc387d1597
並與當前的代碼我的錯誤消息(基本TypeEroor但...): 抱歉,我浪費我的2個小時試圖粘貼我的代碼,我失敗了,所以我生氣了。感謝編輯和答案。
下面是代碼:
## -*- coding=<utf-8> -*-
import json
from json import JSONEncoder
Employees = {}
print(type(Employees))
class Employee(object):
'Common base for all employes'
empCount = 0
def __init__(self,name,salary,age):
self.name = name
self.salary = salary
self.age = age
Employee.empCount += 1
def displayCount(self):
print ("Total Employee : " , Employee.empCount , "\n")
def displayEmployee(self):
print("Name : ", self.name ," Salary : " , self.salary ," Age : " , self.age, "\n")
print ("NEVER FORGET TO SAVE YOUR CHANGES ! \n")
print ("Press s to save your work ! \n")
print ("Press l to load database. \n")
print ("Press x for adding employee \n")
print ("Press y for show employee count \n")
print ("Press z for display employee \n")
print ("Press q for quitting. \n")
while True :
st = input("->> : ")
if (st == "x"):
eid = input ("Id : ")
eName = input ("\nName : ")
eSalary = input ("\nSalary : ")
eAge = input ("\nAge : \n")
Employees[eid] = Employee(eName,eSalary,eAge)
if (st == "y"):
print("Total Employee Count : " , Employee.empCount)
if (st == "z"):
wantedId = input("Give the id : ")
Employees[wantedId].displayEmployee()
if (st == "q"):
exit()
if (st == "s"):
with open('myfile.json','w') as f:
json.dump(dict(Employees),f)
if (st == "l"):
with open('myfile.json') as f:
Employees = json.load(f)
if (st == 'f'):
print("roger dodger")
如果你想讓你的問題得到解答,你可能不應該侮辱你發佈的網站。我建議編輯您的帖子以更有禮貌。 –
請編輯您的問題並刪除最後一段。它的攻擊性與你的問題無關。嘗試再次編輯幷包含您的代碼。你知道,很多人使用這個網站,並且可以寫出格式良好的問題。 – ventiseis