Python 保存数据的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14509269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best method of saving data
提问by Lennart Wijers
I've made a class in which I want to keep track of stats of students. I intend to make a GUI later to manipulate this data.
我做了一门课,我想在其中跟踪学生的统计数据。我打算稍后制作一个 GUI 来操作这些数据。
My main question is: what is the best way to save and later retrieve this data?
我的主要问题是:保存和以后检索这些数据的最佳方法是什么?
I've read about pickle and JSON, but I don't really get how they work (especially about how they save the data, like in which format and where).
我已经阅读了 pickle 和 JSON,但我并没有真正了解它们是如何工作的(尤其是关于它们如何保存数据,例如采用哪种格式和位置)。
采纳答案by Sami N
For persistent data (storing info about students), a database is a good choice. As already mentioned, Python comes shipped with Sqlite3 which is often good enough, for development purposes at least.
对于持久数据(存储有关学生的信息),数据库是一个不错的选择。如前所述,Python 附带 Sqlite3,它通常足够好,至少用于开发目的。
Introducing Sqlite to Python is easy - just import the library in your source code file and open a connection to your database. Refer to the python documentation.
将 Sqlite 引入 Python 很容易 - 只需在源代码文件中导入库并打开与数据库的连接。请参阅 python文档。
EDIT: Found a new tutorialabout Python + Sqlite that seems good.
回答by óscar López
You could use pickling, Python's serialization mechanism:
你可以使用酸洗,Python的序列化机制:
The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” [1] or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”.
pickle 模块实现了一个基本但功能强大的算法,用于序列化和反序列化 Python 对象结构。“pickling”是将 Python 对象层次结构转换为字节流的过程,“unpickling”是逆操作,将字节流转换回对象层次结构。Pickling(和 unpickling)也被称为“序列化”、“编组”、[1] 或“扁平化”,但是,为了避免混淆,这里使用的术语是“酸洗”和“解酸洗”。
回答by Arthur Juli?o
回答by Lennart Regebro
Use a database. SQLAlchemy with SQLight is a good start. You'll end up there in the end anyway.
使用数据库。带有 SQLight 的 SQLAlchemy 是一个好的开始。无论如何,你最终都会在那里结束。
or
或者
Dump everything out with the pickle module. (there really isn't anything to understand, you save objects and then load them again, it's really simple).
使用 pickle 模块转储所有内容。(真的没什么好理解的,你保存对象然后再次加载它们,这真的很简单)。
回答by Brenden Brown
If your data are pretty simple, like just collections of collections of strings or numbers, I would use json. What JSON is, is a string representation of simple data types and combinations of simple data types. Once you use the jsonmodule to convert your data to a string, you write it to a file yourself.
如果您的数据非常简单,例如只是字符串或数字集合的集合,我会使用 json。什么是 JSON,是简单数据类型和简单数据类型组合的字符串表示。一旦您使用该json模块将数据转换为字符串,您就可以自己将其写入文件。
It's super simple:
超级简单:
>>> my_data = [range(5) for i in range(5)]
>>> my_data
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> import json
>>> json.dumps(my_data)
'[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]'
Then just write that string to a file. When you want to reload it, like so:
然后将该字符串写入文件。当你想重新加载它时,像这样:
>>> import json
>>> string_from_file
'[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]'
>>> my_saved_data = json.loads(string_from_file)
>>> my_saved_data
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
If your data are more complicated, and involves classes other than the built-in collection objects, pickle is a better choice. One very importantthing to know about pickle is that there are security vulnerabilities in pickle, and it's a bad idea to unpickle anything you yourself didn't pickle. pickleis vulnerable to the security problems detailed in this article: http://www.kalzumeus.com/2013/01/31/what-the-rails-security-issue-means-for-your-startup/
如果您的数据更复杂,并且涉及内置集合对象以外的类,pickle 是更好的选择。其中一个很重要的了解泡菜的事情是,有泡菜的安全漏洞,这是一个坏主意,unpickle任何你自己没有咸菜。pickle容易受到本文详述的安全问题的影响:http: //www.kalzumeus.com/2013/01/31/what-the-rails-security-issue-means-for-your-startup/
If the size of your data is very large, or you will be saving/loading it frequently, or for any reason using json and saving to a local file is inadequate, then a database will be the way to go.
如果您的数据非常大,或者您将经常保存/加载它,或者出于任何原因使用 json 并保存到本地文件是不够的,那么数据库将是可行的方法。

