【问题标题】:How to pretty print (or pretty format) this Python dictionary which I get from HBase?如何漂亮地打印(或漂亮地格式化)我从 HBase 获得的这个 Python 字典?
【发布时间】: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%'}]}]}

但这并不容易阅读。

【问题讨论】:

标签: python rest hbase


【解决方案1】:

尝试这个:

import pprint

pp = pprint.PrettyPrinter(indent=2)

pp.pprint(d) # d 是你的字典

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 2011-03-14
    • 2020-11-10
    • 2017-02-22
    • 2015-02-07
    • 2017-09-16
    • 2020-10-23
    • 1970-01-01
    • 2010-09-13
    相关资源
    最近更新 更多