打印 Python dict/list 对象后用 FEHelper 查看

用 Python Kubernetes 库查询某个 namespace 下的 Pod 时,返回的结果集嵌套很深,想找到 Pod IP 得翻半天。

问题

直接 print 出来的不是 JSON,Nonedatetime 对象 FEHelper 无法识别;直接 json.dumps 又会报错:

1
TypeError: Object of type datetime is not JSON serializable

解决办法

加一个 default=str 参数即可:

1
2
3
4
5
all_pods = client.CoreV1Api().list_namespaced_pod(
CURRENT_RUNNING_NAMESPACE
).to_dict()["items"]

print(json.dumps(all_pods, sort_keys=True, default=str))

把打印结果拷贝到 FEHelper,去掉头尾的引号就能正常识别,顺利找到 Pod IP 的路径:

1
[s['status']['pod_ip'] for s in all_pods]
Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能要想想办法才能看到。

Powered by Hexo and Hexo-theme-hiker

Copyright © 2012 - 2026 tiaobug.com All Rights Reserved.

鲁ICP备2024124237号-1