Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 存储应用程序持久数据、平面文件或数据库的首选方法是什么?_Python_Local Storage - Fatal编程技术网

Python 存储应用程序持久数据、平面文件或数据库的首选方法是什么?

Python 存储应用程序持久数据、平面文件或数据库的首选方法是什么?,python,local-storage,Python,Local Storage,为Python程序存储特定于应用程序的参数和持久数据的首选方法是什么 我正在创建一个Python程序,它需要存储一些参数:项目名称、开始年份、最大值 我不知道存储这些数据的最佳方式是什么,在进行计算和报告时,我必须重用它们:使用本地TXT文件,使用非常简单的数据库Python中是否存在这些数据?我要用SQLite吗 提前非常感谢。SQLite。非常容易设置,并且可以获得许多内置的db函数。您也不必处理文件读/写和解析。它: pickle模块允许您将大多数Python对象转储到一个文件中,然后重新

为Python程序存储特定于应用程序的参数和持久数据的首选方法是什么

我正在创建一个Python程序,它需要存储一些参数:项目名称、开始年份、最大值

我不知道存储这些数据的最佳方式是什么,在进行计算和报告时,我必须重用它们:使用本地TXT文件,使用非常简单的数据库Python中是否存在这些数据?我要用SQLite吗


提前非常感谢。

SQLite。非常容易设置,并且可以获得许多内置的db函数。您也不必处理文件读/写和解析。

它:

pickle模块允许您将大多数Python对象转储到一个文件中,然后重新读取它们。

您可以使用搁置库。从搁置文档:

工具架是一个持久的、类似字典的对象。与dbm数据库的区别在于,值不是键!在工具架中,基本上可以是任意的Python对象——pickle模块可以处理的任何对象

import shelve
d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
d[key] = data   # store data at key (overwrites old data if
                # using an existing key)
data = d[key]   # retrieve a COPY of the data at key (raise
                # KeyError if no such key) -- NOTE that this
                # access returns a *copy* of the entry!
del d[key]      # delete data stored at key (raises KeyError
                # if no such key)
flag = d.has_key(key)   # true if the key exists; same as "key in d"
list = d.keys() # a list of all existing keys (slow!)
d.close()

若方案是固定的,那个么sqldb是最好的选择,就像sqlite3一样,加上memcached作为缓存。
如果关系经常改变,我认为灵活的数据可能会存储在文件中。散列索引。

Hi Blender。非常感谢你的回答。如果我想在程序的不同时刻存储参数,我可以使用…options={[code]'project_name':'foo','start_year':2000}options={'project_name':'foo','start_year':2000}[/code]?
import shelve
d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
d[key] = data   # store data at key (overwrites old data if
                # using an existing key)
data = d[key]   # retrieve a COPY of the data at key (raise
                # KeyError if no such key) -- NOTE that this
                # access returns a *copy* of the entry!
del d[key]      # delete data stored at key (raises KeyError
                # if no such key)
flag = d.has_key(key)   # true if the key exists; same as "key in d"
list = d.keys() # a list of all existing keys (slow!)
d.close()