如何格式化漫游器“ send_message”输出的格式,使其与表格对齐?

时间:2019-05-07 20:59:22

标签: python-3.x telegram-bot python-telegram-bot

我正在尝试使用精彩的python-telegram-bot模块创建一个简单的Telegram Bot,但是我无法使字典与默认的"{0:<20} {1}".format(key, value)想法保持一致。

让我举个例子:

MAP = {
    "one": "1",
    "two": "2",
    "three": "3",
    "four": "4",
    "five": "5",
    "six": "6",
    "seven": "7",
    "eight": "8"
}

tmpstring = ""

for key, value in MAP.items():
    tmpstring = tmpstring + "{0:<20} {1}".format(key, value) + "\n"

print(tmpstring)
context.bot.send_message(chat_id=update.message.chat_id, text=tmpstring)

印刷后的外观如下:

one                  1
two                  2
three                3
four                 4
five                 5
six                  6
seven                7
eight                8

如预期的那样完美地对齐,但是Telegram中的消息看起来像这样:

enter image description here

所以我的问题是: 如何调整聊天消息的外观,使其看起来像打印的输出?

1 个答案:

答案 0 :(得分:1)

我总是使用pre-formatted降价样式来保留对齐方式。 (Docs

完全对齐表括在引号中


> ```
one          1
two          2
three        3

> ```

注意:在>之前删除`

使用MarkDown parsemode发送消息

相关问题