如何格式化 bot 'send_message' 输出,使其像表格一样对齐?

Lyu*_*yux 4 python-3.x python-telegram-bot 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)
Run Code Online (Sandbox Code Playgroud)

印刷完成看起来像这样:

one                  1
two                  2
three                3
four                 4
five                 5
six                  6
seven                7
eight                8
Run Code Online (Sandbox Code Playgroud)

正如预期的那样完美对齐,但 Telegram 中的消息如下所示:

在此处输入图片说明

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

0st*_*ne0 14

使用Markdowns (V2) 'pre-formatted fixed-width code block'来保持对齐。[文档]

使用3 个反引号 ( )将完美对齐的表格包装在代码块中```

```
one          1
two          2
three        3
```
Run Code Online (Sandbox Code Playgroud)

发送您的消息,parse_mode选项设置为:MarkDown (V2)