所以我为我的对象帐户提供了一个函数,一旦它创建自己,它就会将自己记录到一个文件中,但它似乎不起作用。我不断收到错误,类型错误:类型"getset_descriptor"的对象不可 JSON 序列化。提前感谢您的任何建议。
import os
import json
import datetime
clear = lambda: os.system("cls")
now = datetime.date
temp = 1
temp_intger = None
class Account:
def __init__(self , account_number , account_balance , account_creation , account_firstname , account_lastname):
self.acc_number = account_number
self.acc_balance = account_balance
self.acc_creation = account_creation
self.acc_firstname = account_firstname
self.acc_lastname = account_lastname
def account_creation():
acc = {}
acc["account_firstname"] = input("Please enter the first name of the account")
acc["account_lastname"] = input("Please enter the last name of the account")
acc["account_balance"] = float(input("Please enter the balance of the account"))
acc["account_creation"] = now.day
while True:
temp = input("Please enter a 4 pin number for your account!")
if ((os.path.isfile("%s.json" %(temp))) == True):
continue
else:
break
with open("%s.json" % (temp) , "w") as fp:
json.dump(acc,fp)
print ("Done!")
account_creation()
TypeError:类型为"getset_descriptor"的对象不是 JSON 可序列化的,这是由于您的 date(now = datetime.date,now.day())您的 JSON 无法序列化您的日期对象来克服这个问题,您需要修改日期到字符串
now = str(datetime.date)
和acc["account_creation"] = str(now.day)
你的麻烦在第6行:now = datetime.date
看起来您正在尝试使用当前时间创建一个日期时间对象,但这不是您对该行所做的。
我相信你想now = datetime.datetime.now()
那里。