【发布时间】:2022-11-18 01:39:28
【问题描述】:
我有一个 Python 字典对象 row,其中包含一些字节对象。
所以当我尝试做json.dumps(row, indent = 4)
我收到这个错误
TypeError: Object of type bytes is not JSON serializable
现在我的问题是这个row 对象来自 HBase,所以我无法控制它。
我正在使用这个库
https://pypi.org/project/hbase-rest-py/
它连接到 HBase REST API 并返回给我这个 row dict 对象。
它类似于 JSON 但包含字节对象。
那么,如果它是一个字典但这个字典包含一些字节对象,我该如何漂亮地打印它呢?!
如果我只是打印它(使用 Python 的内置打印功能),我会得到:
{'row': [{'key': b'B70DA612B08299967EB536F9EAF88CB4_20221115_685611_DESKTOP_SEARCH_SEARCH', 'cell': [{'column': b'p:clk', 'timestamp': 1668687955736, '$': b'2'}, {'column': b'p:con1', 'timestamp': 1668687955736, '$': b'0.0000'}, {'column': b'p:cst', 'timestamp': 1668687955736, '$': b'7140000.000000'}, {'column': b'p:cwm', 'timestamp': 1668687955736, '$': b'16.604651'}, {'column': b'p:eis', 'timestamp': 1668687955736, '$': b'42.86%'}, {'column': b'p:imp', 'timestamp': 1668687955736, '$': b'25'}, {'column': b'p:se', 'timestamp': 1668687955736, '$': b'2'}, {'column': b'p:sis', 'timestamp': 1668687955736, '$': b'13.59%'}, {'column': b'p:sisb', 'timestamp': 1668687955736, '$': b'9.78%'}, {'column': b'p:sisr', 'timestamp': 1668687955736, '$': b'76.63%'}]}]}
但这并不容易阅读。
【问题讨论】:
-
这回答了你的问题了吗? How to prettyprint a JSON file?
-
试试
pprint而不是print