我正在研究一个简单的员工系统,用于在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