如何获得对象数据库

如何获得对象数据库

本文介绍了如何获得对象数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能推荐一个模块或告诉我为Python获取对象数据库的方法吗?
我对数据库中保存对象感兴趣的原因之一是因为我想数据库可以提供比使用 pickle 更简单的代码。
在我的问题中有人使用 pickle 给了我一段代码,我认为应该有一种保存对象的简单方法。

Can anyone recommend a module or tell me way to get a object database for Python?One of the reason I´m interested in databases to save objects is because I suppose a database can provide a simpler piece of code than use pickle.In my question Saving an Object (Data persistence) someone gave me a piece of code using pickle and I think there should be a simpler way to save objects.

推荐答案

如果您最关心的是代码的简单性,那么 pickle 很难被克服。

If the simplicity of the code is your main concern, pickle is hard to beat.

基本上,要保存对象数据,要做的就是:

Basically, all you have to do in order to save an object data is:

with open('data.pickle', 'w') as pickle_file:
    pickle.dump(data, pickle_file)

然后您可以通过以下方式将其取回:

You can then get it back with:

with open('data.pickle') as pickle_file:
    data = pickle.load(pickle_file)  # You get the data back!

有,如果您需要保存自己的 own 类中的对象,则可能要查看将对象保存到文件中基本上与上面的编写/读取代码一样简单!

There are some technical details that you might want to check out, if you need to save objects from your own classes, but saving objects to a file is basically as simple as the writing/reading code above!

这篇关于如何获得对象数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 15:37