将 Prettytable 的输出发送到 Telegram
Sending the output of Prettytable to Telegram
我有以下代码:
来自 prettytable 导入 PrettyTable
myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
# Add rows
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,如下所示:
+--------------+-------+---------+------------+
| 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 % |
+--------------+-------+---------+------------+
我想发送此 table 原样,不更改电报消息的格式。所以我把它写到一个文本文件中:
table_txt = table.get_string()
with open('output.txt','w') as file:
file.write(table_txt)
接下来我使用这段代码:
import telegram
def send_msg(text):
token = "******************:***********"
chat_id = "********"
bot = telegram.Bot(token=token)
for i in text:
bot.sendMessage(chat_id=chat_id, text=i)
new_list = []
with open("output.txt", 'r', encoding="utf-8") as file:
new_list = file.read()
for i in new_list:
send_msg()
它所做的是一次发送 1 个字符的文件内容,直到收到电报错误:
RetryAfter: Flood control exceeded. Retry in 43.0 seconds
请告知我该怎么做才能解决这个问题?
我不是 python 开发人员,但我认为这可行。
import telegram
def send_msg(text):
token = "******************:***********"
chat_id = "********"
bot = telegram.Bot(token=token)
bot.sendMessage(chat_id=chat_id, text=text)
with open("output.txt", 'r', encoding="utf-8") as file:
send_msg(file.read())
问题是这段代码:
for i in text:
bot.sendMessage(chat_id=chat_id, text=i)
您不是在发送文本,您是在迭代文本。通过这样做,您将获得整个字符串的子字符串,每个子字符串仅包含一个字符:
text = "hello"
outputs = []
for i in text:
outputs.append(i)
assert outputs == ['h', 'e', 'l', 'l', 'o']
for i in text:
print(i)
# h
# e
# l
# l
# o
要解决此问题,您可以只发送整个字符串:
bot.sendMessage(chat_id=chat_id, text=text)
我有以下代码:
来自 prettytable 导入 PrettyTable
myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
# Add rows
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,如下所示:
+--------------+-------+---------+------------+
| 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 % |
+--------------+-------+---------+------------+
我想发送此 table 原样,不更改电报消息的格式。所以我把它写到一个文本文件中:
table_txt = table.get_string()
with open('output.txt','w') as file:
file.write(table_txt)
接下来我使用这段代码:
import telegram
def send_msg(text):
token = "******************:***********"
chat_id = "********"
bot = telegram.Bot(token=token)
for i in text:
bot.sendMessage(chat_id=chat_id, text=i)
new_list = []
with open("output.txt", 'r', encoding="utf-8") as file:
new_list = file.read()
for i in new_list:
send_msg()
它所做的是一次发送 1 个字符的文件内容,直到收到电报错误:
RetryAfter: Flood control exceeded. Retry in 43.0 seconds
请告知我该怎么做才能解决这个问题?
我不是 python 开发人员,但我认为这可行。
import telegram
def send_msg(text):
token = "******************:***********"
chat_id = "********"
bot = telegram.Bot(token=token)
bot.sendMessage(chat_id=chat_id, text=text)
with open("output.txt", 'r', encoding="utf-8") as file:
send_msg(file.read())
问题是这段代码:
for i in text:
bot.sendMessage(chat_id=chat_id, text=i)
您不是在发送文本,您是在迭代文本。通过这样做,您将获得整个字符串的子字符串,每个子字符串仅包含一个字符:
text = "hello"
outputs = []
for i in text:
outputs.append(i)
assert outputs == ['h', 'e', 'l', 'l', 'o']
for i in text:
print(i)
# h
# e
# l
# l
# o
要解决此问题,您可以只发送整个字符串:
bot.sendMessage(chat_id=chat_id, text=text)