使用 Python 进行基本数据存储
我需要存储客户和他们购买的汽车的基本数据以及这些汽车的付款时间表。这些数据来自 GUI,用 Python 编写。我没有足够的经验来使用像sql这样的数据库系统,所以我想将我的数据以纯文本形式存储在文件中。而且它不必在线。
为了能够搜索和过滤它们,首先我将数据(列表列表)转换为字符串,然后当我需要数据时重新转换为常规 Python 列表语法。我知道这是一种非常暴力的方式,但是这样做安全吗?或者你可以建议我采用另一种方式吗?
I need to store basic data of customer's and cars that they bought and payment schedule of these cars. These data come from GUI, written in Python. I don't have enough experience to use a database system like sql, so I want to store my data in a file as plain text. And it doesn't have to be online.
To be able to search and filter them, first I convert my data (lists of lists) to the string then when I need the data re-convert to the regular Python list syntax. I know it is a very brute-force way, but is it safe to do like that or can you advice me to another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
以文本格式(或使用 pickle 或其他方式)保存数据库永远不安全。保存数据时出现问题可能会导致损坏。更不用说您的数据被盗的风险。
随着数据集的增长,性能可能会受到影响。
看看 sqlite(或 sqlite3),它比 mysql 小且更易于管理。除非您有一个非常小的数据集,可以放入文本文件中。
P/S:顺便说一句,在 python 中使用 berkeley db 很简单,你不必学习所有 DB 的东西,只需 import bsddb
It is never safe to save your database in a text format (or using pickle or whatever). There is a risk that problems while saving the data may cause corruption. Not to mention risks with your data being stolen.
As your dataset grows there may be a performance hit.
have a look at sqlite (or sqlite3) which is small and easier to manage than mysql. Unless you have a very small dataset that will fit in a text file.
P/S: btw, using berkeley db in python is simple, and you don't have to learn all the DB things, just import bsddb
使用pickle的答案很好,但我个人更喜欢搁置。它允许您将变量保持在启动之间的相同状态,我发现它比直接使用 pickle 更容易使用。 http://docs.python.org/library/shelve.html
The answer to use pickle is good, but I personally prefer shelve. It allows you to keep variables in the same state they were in between launches and I find it easier to use than pickle directly. http://docs.python.org/library/shelve.html
我同意其他人的观点,即严肃而重要的数据在某种类型的轻型数据库中会更安全,但也可以同情保持事情简单和透明的愿望。
因此,我建议您不要发明自己的基于文本的数据格式,而是使用 YAML
该格式是人类可读的示例:
您像这样加载文件:
列表将如下所示:
当然您也可以执行相反的操作并将数据保存到 YAML 中,文档将帮助您完成此操作。
至少可以考虑另一种选择:)
I agree with the others that serious and important data would be more secure in some type of light database but can also feel sympathy for the wish to keep things simple and transparent.
So, instead of inventing your own text-based data-format I would suggest you use YAML
The format is human-readable for example:
You load the file like this:
And list will look like this:
Of course you can do the reverse too and save data into YAML, the docs will help you with that.
At least another alternative to consider :)
非常简单和基本 - (更多@ http://pastebin.com/A12w9SVd)
使用:
very simple and basic - (more @ http://pastebin.com/A12w9SVd)
using:
您可以使用此库将对象写入文件 http://docs.python.org/库/pickle.html
You can use this lib to write an object into a file http://docs.python.org/library/pickle.html
将数据写入文件并不是一种安全的数据存储方式。最好使用简单的数据库库,例如 sqlalchemy。它是一个易于数据库使用的 ORM...
Writing data in a file isn't a safe way for datastorage. Better use a simple database libary like sqlalchemy. It is a ORM for easy database usage...
您还可以将简单数据保存在纯文本文件中。但是,那么您就没有太多支持来检查数据、双值等的一致性。
这是文本文件中我的简单“卡片文件”类型数据 代码片段使用namedtuple,这样您不仅可以通过行中的索引访问值,还可以通过标头名称访问值:
You can also keep simple data in plain text file. Then you have not much support, however, to check consistency of data, double values etc.
Here is my simple 'card file' type data in text file code snippet using namedtuple so that you can access values not only by index in line but by they header name: