Python字典或JSON [英] Python Dictionary or JSON
问题描述
将大型嵌套的Python字典转换为JSON以在小型库中使用(即不在网络上使用)是否有好处?
我正在解析配置文件并存储不同的数据位,以供以后检索和输出.当前,所有信息都存储在嵌套字典中,列表中也有列表.并不是说这种方法一定存在问题,我只是想知道使用标准数据格式是否有益,而且嵌套的dict看起来很像JSON.
这是苹果与桔子的比较:JSON是一种数据格式(字符串),Python字典是一种数据结构(内存对象).>
如果您需要在不同(甚至是非Python)进程之间交换数据,则可以使用JSON格式序列化您的Python字典.
字典的文本表示形式类似于(但不是)json格式:
>>> print(dict(zip('abc', range(3))))
{'a': 0, 'b': 1, 'c': 2}
对象的文本表示形式(字符串)不是对象本身(即使字符串对象及其文本表示形式也不同,例如,"\n"
是单个换行符,但显然其文本表示形式是多个字符).
Is there any benefit to converting a large nested Python dictionary to JSON for using within a small library (ie. not over the web)?
I am parsing configuration files and storing different bits of data for retrieval and output later. Currently all the information is stored in a nested dict, with lists in there also. It's not that there's a problem with this method necessarily, I just wonder if it's beneficial to use a standard data format, and the nested dict looks an awful lot like JSON.
It is apples vs. oranges comparison: JSON is a data format (a string), Python dictionary is a data structure (in-memory object).
If you need to exchange data between different (perhaps even non-Python) processes then you could use JSON format to serialize your Python dictionary.
The text representation of a dictionary looks like (but it is not) json format:
>>> print(dict(zip('abc', range(3))))
{'a': 0, 'b': 1, 'c': 2}
Text representation (a string) of an object is not the object itself (even string objects and their text representations are different things e.g., "\n"
is a single newline character but obviously its text representation is several characters).
这篇关于Python字典或JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!