我使用正确的缩进将字典data
转换为html。当前,我使用以下方法:
import json
from IPython.core.display import display, HTML
data = {'a': 1,
'b': {'c': 2, 'd': 3}}
# Convert to string
data_html = json.dumps(data, indent=4)
# Add html spaces
data_html = data_html.replace(" ", " ")
# Add html next lines
data_html = data_html.replace("\n", "<br />\n")
display(HTML(data_html))
输出:
{
"a": 1,
"b": {
"c": 2,
"d": 3
}
}
是否有更好/更清洁的方法来实现此结果?