python中将对象存入字典_如何在Python中保存对象字典?

I have a Python 3.5 program that creates an inventory of objects. I created a class of Trampolines (color, size, spring, etc.). I constantly will create new instances of the class and I then save a dictionary of them. The dictionary looks like this:

my_dict = {name: instance} and the types are like so {"string": "object"}

MY ISSUE is that I want to know how to save this inventory list so that I can start where I left off the last time I closed the program.

I don't want to use "pickle" because I'm trying to learn secure ways to do this for more important versions in the future.

I thought about using SQLite3, so any tips on how to do this easily would be appreciated.

My preferred solution would state how to do it with the "json" module. I tried it, but the error I got was

__main__.Trampoline object at 0x00032432... is not JSON serializable

EDIT:

Below is the code I used when I got the error:

out_file = open(input("What do you want to save it as? "), "w")

json.dump(my_dict, out_file, indent=4)

out_file.close()

END of EDIT

I've done a good amount of research, and saw that there's also an issue with many of these SAVE options that you can only do ONE object per 'save file', but that the work around to this is that you use a dictionary of objects, such as the one I made. Any info clarifying this would be great, too!

Thanks!

解决方案

Here is an example of a class that handles datetime objects.

class CustomEncoder(json.JSONEncoder):

def default(self, obj):

if isinstance(obj, datetime.datetime):

if obj.tzinfo:

obj = obj.astimezone(isodate.tzinfo.UTC).replace(tzinfo=None)

return obj.isoformat()[:23] + 'Z'

return json.JSONEncoder.default(self, obj)

when you encode to json the default function of the cls is called with object you passed. If you want to handle a type that is not part of the standard json.JSONEncoder.default you need to intercept it and return how you want it handled as a valid json type. In this example I turned the datetime into a str and returned that. If its not one of the types I want to special case, I just pass it along to the standard json.JSONEncoder.default handler.

To use this class you need to pass it in the cls param of json.dump or json.dumps:

json.dumps(obj, cls=CustomEncoder)

Decoding is done the same way but with json.JSONDecoder, json.load, and json.loads. However you can not match on type, so you will need to either add an 'hint' in encoding for decoding or know what type it needs to decode.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值