【发布时间】:2021-05-18 21:15:00
【问题描述】:
我正在尝试通过调用来漂亮地打印一个 python 对象:
from pprint import pprint
...
pprint(update)
但输出看起来像这样:
<telegram.update.Update object at 0xffff967e62b0>
但是,使用 Python 的内部 print() 我得到了正确的输出:
{'update_id': 14191809, 'message': {'message_id': 22222, 'date': 11111, 'chat': {'id': 00000, 'type': 'private', 'username': 'xxxx', 'first_name': 'X', 'last_name': 'Y'}, 'text': '/start', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 6}], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 01010101, 'first_name': 'X', 'is_bot': False, 'last_name': 'Y', 'username': 'xxxx', 'language_code': 'en'}}}
有没有办法制作pprint(),正确显示对象数据并格式化?
【问题讨论】:
-
pprint(update.__str__())? -
@PacketLoss 不幸的是,该问题的答案并未提供我正在寻找的行为。不过
print()函数打印出来的数据还是比较完整的。 -
@CryptoFool 你不能
pprint一个字符串。好吧,你可以,但它只会换行。也不要直接调用__str__,而是使用str(...)。
标签: python pretty-print python-telegram-bot