将python dict转换为字符串并返回 [英] Convert a python dict to a string and back

查看:128
本文介绍了将python dict转换为字符串并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将数据存储在字典对象中的程序,但是在程序执行期间,这些数据需要保存,并在程序再次运行时加载到字典对象中。
我将如何将字典对象转换成可写入文件并加载到字典对象的字符串?这将有望支持包含字典的字典。

解决方案

json模块是一个很好的解决方案,产生纯文本输出,并且是跨平台和跨版本。


I am writing a program that stores data in a dictionary object, but this data needs to be saved at some point during the program execution and loaded back into the dictionary object when the program is run again. How would I convert a dictionary object into a string that can be written to a file and loaded back into a dictionary object? This will hopefully support dictionaries containing dictionaries.

解决方案

json module is a good solution here, and has the advantage over pickle that it only produces plain text output, and is cross-platform and cross-version.

这篇关于将python dict转换为字符串并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆