问题描述
我正在编写一个网站,用户将在其中进行多种设置,例如他们选择的配色方案等.我很乐意将这些设置为纯文本文件,而且安全性不是问题.
I am programming a website in which users will have a number of settings, such as their choice of colour scheme, etc. I'm happy to store these as plain text files, and security is not an issue.
我目前的看法是:有一个字典,其中所有的键都是用户,值是包含用户设置的字典.
The way I currently see it is: there is a dictionary, where all the keys are users and the values are dictionaries with the users' settings in them.
例如,userdb["bob"]["colour_scheme"] 的值为blue".
For example, userdb["bob"]["colour_scheme"] would have the value "blue".
将其存储在文件中的最佳方式是什么?酸洗字典?
What is the best way to store it on file? Pickling the dictionary?
是否有更好的方法来做我想做的事情?
Are there better ways of doing what I am trying to do?
推荐答案
使用 cPickle 将是我的选择.字典非常适合这类数据,因此鉴于您的要求,我认为没有理由不使用它们.除非您考虑从非 Python 应用程序中读取它们,否则您必须使用语言中性文本格式.即使在这里,您也可以使用泡菜和导出工具.
Using cPickle on the dictionary would be my choice. Dictionaries are a natural fit for these kind of data, so given your requirements I see no reason not to use them. That, unless you are thinking about reading them from non-python applications, in which case you'd have to use a language neutral text format. And even here you could get away with the pickle plus an export tool.
这篇关于在 Python 中存储简单的用户设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!