python - 如何将 Python 内存字典作为 Python 源代码保存到文件中?

标签 python serialization configuration dictionary code-generation

如何获取内存中 Python 字典的 Python 源代码 表示?

我决定在阅读 Thomas Kluyver 的 comment 后问这个问题在 Rob Galanakis 的博客文章中,标题为 Why bother with python and config files?托马斯在他的评论中说

But if you want any way to change settings inside the application (like a preferences dialog), there’s no good way to automatically write a correct Python file.

最佳答案

假设它只使用“基本”Python 类型,你可以写出结构的repr(),然后使用ast.literal_eval() 读取它之后回来。

关于python - 如何将 Python 内存字典作为 Python 源代码保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11594304/

相关文章:

asp.net - 什么基准可以测试我的 ASP.NET、SQL Server、IIS 产品的硬件性能?

configuration - Windows Azure : how do I expose a configuration setting as an environment variable?

Python 批量重命名器可以工作,但不能同时工作?

python - 如何在 Spyder 中使用代码格式化程序 Black?

python - 如何定义类何时为空

windows - Windows 上的检查点(快照/恢复)库?

java - 如何使用子gradle模块的application.properties?

python - 如何在 linux 上为 anaconda 安装 ipdb?

php - 将 session $data 转换为数组?

java - 将 ArrayList 保存到文件