Python人类可读对象序列化
-
03-07-2019 - |
题
我需要将由列表/字典组成的Python结构,元组存储为人类可读的格式。这个想法就像使用类似于 pickle 的东西,但是pickle不是人类友好的。我想到的其他选择是 YAML (通过 PyYAML 和 JSON (通过 simplejson )序列化程序。
您想到的任何其他选项?
提前致谢。
解决方案
对于简单的情况,可以想到pprint()和eval()。
使用您的示例:
>>> d = {'age': 27,
... 'name': 'Joe',
... 'numbers': [1,
... 2,
... 3,
... 4,
... 5],
... 'subdict': {
... 'first': 1,
... 'second': 2,
... 'third': 3
... }
... }
>>>
>>> from pprint import pprint
>>> pprint(d)
{'age': 27,
'name': 'Joe',
'numbers': [1, 2, 3, 4, 5],
'subdict': {'first': 1, 'second': 2, 'third': 3}}
>>>
我会考虑使用相同的工具修复两个要求。您是否考虑过使用pickle进行序列化,然后使用pprint()(或更精美的对象查看器)来查看对象?
其他提示
如果它的只是 Python列表,字典和元组对象。 - JSON 是要走的路。它具有人类可读性,非常易于处理和语言独立。
警告:元组将在simplejson中转换为列表。
In [109]: simplejson.loads(simplejson.dumps({'d':(12,3,4,4,5)}))
Out[109]: {u'd': [12, 3, 4, 4, 5]}
如果您的演示文稿多于JSON所涵盖的代表,我强烈建议您查看 PyON (Python Object Notation)...虽然我认为它仅限于2.6 / 3.0及更高版本,因为它依赖于 ast 模块。它处理自定义类实例和递归数据类型以及其他功能,这些功能不仅仅是由JSON提供的。
你应该看看jsonpickle( https://github.com/jsonpickle/jsonpickle )。它会将任何python对象写入json文件。然后,您可以将该文件读回python对象。好处是inbetween文件非常易读,因为它是json。
你的意思是什么,这不是人类可读的??? ;)
>>> d = {'age': 27,
... 'name': 'Joe',
... 'numbers': [1,2,3,4,5],
... 'subdict': {'first':1, 'second':2, 'third':3}
... }
>>>
>>> import pickle
>>> p = pickle.dumps(d)
>>> p
"(dp0\nS'age'\np1\nI27\nsS'subdict'\np2\n(dp3\nS'second'\np4\nI2\nsS'third'\np5\nI3\nsS'first'\np6\nI1\nssS'name'\np7\nS'Joe'\np8\nsS'numbers'\np9\n(lp10\nI1\naI2\naI3\naI4\naI5\nas."
好吧,也许它只需要一些练习…或者你可以作弊......
>>> import pickletools
>>> pickletools.dis(p)
0: ( MARK
1: d DICT (MARK at 0)
2: p PUT 0
5: S STRING 'age'
12: p PUT 1
15: I INT 27
19: s SETITEM
20: S STRING 'subdict'
31: p PUT 2
34: ( MARK
35: d DICT (MARK at 34)
36: p PUT 3
39: S STRING 'second'
49: p PUT 4
52: I INT 2
55: s SETITEM
56: S STRING 'third'
65: p PUT 5
68: I INT 3
71: s SETITEM
72: S STRING 'first'
81: p PUT 6
84: I INT 1
87: s SETITEM
88: s SETITEM
89: S STRING 'name'
97: p PUT 7
100: S STRING 'Joe'
107: p PUT 8
110: s SETITEM
111: S STRING 'numbers'
122: p PUT 9
125: ( MARK
126: l LIST (MARK at 125)
127: p PUT 10
131: I INT 1
134: a APPEND
135: I INT 2
138: a APPEND
139: I INT 3
142: a APPEND
143: I INT 4
146: a APPEND
147: I INT 5
150: a APPEND
151: s SETITEM
152: . STOP
highest protocol among opcodes = 0
>>>
你仍然需要从文件中读取pickled对象,但是你不需要加载
它。所以,如果它是“危险的”,那么对象,您仍然可以在执行 load
之前弄清楚这一点。如果您遇到 pickle
,那么解密您所拥有的内容可能是个不错的选择。
首先使用simplejson easy_install simplejson :
import simplejson
my_structure = {"name":"Joe", "age":27, "numbers":[1,2,3,4,5], "subdict":{"first":1, "second":2, "third": 3}}
json = simplejson.dumps(my_structure)
导致json成为:
{"age": 27, "subdict": {"second": 2, "third": 3, "first": 1}, "name": "Joe", "numbers": [1, 2, 3, 4, 5]}
请注意,它根本没有改变字典的格式,但您应该通过此步骤运行它以确保有效的JSON数据。
您可以进一步打印结果:
import pprint
pprint.pprint(my_structure)
结果:
{'age': 27,
'name': 'Joe',
'numbers': [1, 2, 3, 4, 5],
'subdict': {'first': 1, 'second': 2, 'third': 3}}
AXON (文字)格式结合了最佳 JSON,XML和YAML。 AXON格式非常易读且相对紧凑。
python(2.7 / 3.3-3.7)模块 pyaxon 支持 load( s)
/ dump(s)
功能,包括迭代 loading
/ dumping
。它足够快,以便有用。
考虑简单的例子:
>>> d = {
'age': 27, 'name': 'Joe',
'numbers': [1, 2, 3, 4, 5],
'subdict': {'first': 1, 'second': 2, 'third': 3}
}
# pretty form
>>> axon.dumps(d, pretty=1)
{ age: 27
name: "Joe"
numbers: [1 2 3 4 5]
subdict: {
first: 1
second: 2
third: 3}}
# compact form
>>> axon.dumps(d)
{age:27 name:"Joe" numbers:[1 2 3 4 5] subdict:{first:1 second:2 third:3}}
它还可以处理消息中的多个对象:
>>> msg = axon.dumps([{'a':1, 'b':2, 'c':3}, {'a':2, 'b':3, 'c':4}])
>>> print(msg)
{a:1 b:2 c:3}
{a:2 b:3 c:4}
{a:3 b:4 c:5}
然后迭代加载它们:
for d in axon.iloads(msg):
print(d)