我有一个 python 脚本,它以 json 的形式获取集群运行状况并向我发送邮件。问题是 json 打印不漂亮。 这些是我已经尝试过的方法:
- 简单 --> json.dumps(health)
- json.dumps(health, indent=4, sort_keys=True)
但是gmail中的输出还是没有格式化,有点像这样
{ “active_primary_shards”:25,“active_shards”:50,“active_shards_percent_as_number”:100.0,“cluster_name”:“number_of_pending_tasks”:0,“relocating_shards”:0,“status”:“green”,“task_max_waiting_in_queue_millis": 0, "timed_out": false, "unassigned_shards": 0
邮件已发送到 gmail
最佳答案
我不能肯定地说,但看起来您的电子邮件发送代码默认发送“HTML”电子邮件,并且在 HTML 中连续的空格合并为一个,这样 HTML 代码如下:
<p>
This is a paragraph, but it's long so
I'll break to a new line, and indented
so I know it's within the `p` tag, etc.
</p>
看起来像“这是一个段落,但它很长所以我会换行,并缩进以便我知道它在 p
标签内,等等。”给用户。
所以,我想说你的两个选择是:
- 更改电子邮件发送代码以发送
Content-type
标题为text/plain
, 或 用
替换所有空格(不间断空格)字符和换行符<br>
(休息),例如:email_body = json.dumps( health, indent=4, sort_keys=True).replace(' ', ' ').replace('\n', '<br>')
关于python - 如何将 json 对象漂亮地打印为电子邮件 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41458580/