简体   繁体   English

独立python应用程序的数据存储

[英]Data storage for standalone python application

I want to make a python program (with a PyQt GUI, but I don't know whether that is relevant) that has to save some information that I want to store even when the program closes. 我想制作一个python程序(带有PyQt GUI,但我不知道这是否相关),即使程序关闭,它也必须保存一些我想存储的信息。 Example for information I want to store: 我要存储的信息示例:

  1. The user can search for a file in a file dialog window. 用户可以在文件对话框窗口中搜索文件。 I want to start the file dialog window in the previously used directory , even if the program is closed in between file searches. 我想在以前使用的目录中启动文件对话框窗口,即使该程序在两次文件搜索之间都已关闭。

  2. The user can enter their own categories to sort items, building up on some of my predefined categories. 用户可以输入自己的类别来对项目进行排序,以我的一些预定义类别为基础。 These new categories should be available the next time the program starts. 这些新类别应该在下次程序启动时可用。

Now I'm wondering what the proper way to store such information is. 现在,我想知道存储此类信息的正确方法是什么。 Should I use pickle? 我应该用泡菜吗? A proper database (I know a tiny bit of sqlite3, but would have to read up on that)? 一个合适的数据库(我知道一点sqlite3,但必须对此进行阅读)? A simple text file that I parse myself? 我自己解析的一个简单文本文件? One thing for data like in example 1., another for data like in example 2.? 一件事像示例1中的数据,另一件事类似于示例2中的数据?

Also, whatever way to store it I use, where would I put that file? 另外,无论使用哪种存储方式,我都将该文件放在哪里?

I'm asking in the context that I might want to later make my program available to others as a standalone application (using py2app, py2exe or PyInstaller). 我在上下文中询问,我以后可能想让我的程序作为独立的应用程序(使用py2app,py2exe或PyInstaller)供其他人使用。

Right now I'm just saving a pickle file in the directory that my .py file is in, like this answer reconmends, but the answer also specifically mentions: 现在,我只是将一个pickle文件保存在我的.py文件所在的目录中,就像这个答案一样,但答案还特别提到:

for a personal project it might be enough. 对于个人项目,这可能就足够了。

(emphasis mine) (强调我的)

Is using pickle also the "proper, professional" way, if I want to make the program available to other people as a standalone application? 如果我想将程序作为独立的应用程序提供给其他人,使用pickle还是“适当,专业”的方法吗?

I think SQLite3 is the better solution in this case as Moldovan commented. 正如摩尔多瓦所说,我认为在这种情况下,SQLite3是更好的解决方案。

There is a problem in pickle, sometimes pickling format can be change across python versions and there are greater advantages of using sqlite3. 泡菜有问题,有时在python版本之间可以更改酸洗格式,并且使用sqlite3具有更大的优势。

Choice depends on your approach to data you store, which is yours?: 选择取决于您存储数据的方式,这是您的?:

  • user should be able to alter it without usage of my program 用户应该能够在不使用我的程序的情况下进行更改
  • user should be prevented from altering it with program other than my program 应防止用户使用我程序以外的程序对其进行更改

If first you might consider deploying JSON open-standard file format, for which Python has ready library called json . 如果首先,您可以考虑部署JSON开放标准文件格式,Python已为此文件准备了名为json的库。 In effect you get text (which you can save to file) which is human-readable and can be edited in text editor. 实际上,您将获得人类可读的文本(可以将其保存到文件中),并且可以在文本编辑器中对其进行编辑。 Also there exist JSON file viewers and editors which made viewing/editing of JSON files easier. 此外,还存在JSON文件查看器和编辑器,它们使JSON文件的查看/编辑更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM