【发布时间】:2021-08-13 16:39:06
【问题描述】:
Sending the output of Prettytable to Telegram
此问题是对先前问题的后续问题。我的代码是这样的:
import telegram
from prettytable import PrettyTable
def send_msg(text):
token = "*******:**************"
chat_id = "***********"
bot = telegram.Bot(token=token)
bot.sendMessage(chat_id=chat_id, text=text)
myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
myTable.add_row(["Leanord", "X", "B", "91.2 %"])
myTable.add_row(["Penny", "X", "C", "63.5 %"])
myTable.add_row(["Howard", "X", "A", "90.23 %"])
myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
myTable.add_row(["Raj", "X", "B", "88.1 %"])
myTable.add_row(["Amy", "X", "B", "95.0 %"])
table_txt = myTable.get_string()
with open('output.txt','w') as file:
file.write(table_txt)
new_list = []
with open("output.txt", 'r', encoding="utf-8") as file:
send_msg(file.read())
问题是发送的消息是这样的:
+--------------+-------+---------+------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
| Leanord | X | B | 91.2 % |
| Penny | X | C | 63.5 % |
| Howard | X | A | 90.23 % |
| Bernadette | X | D | 92.7 % |
| Sheldon | X | A | 98.2 % |
| Raj | X | B | 88.1 % |
| Amy | X | B | 95.0 % |
+--------------+-------+---------+------------+
但是当电报中收到的消息是这样的:
我该如何解决这个问题? Telegram 允许您在code 中发送消息,我猜它会保留格式。如何以 格式发送此消息?
【问题讨论】:
标签: python telegram python-telegram-bot