For my needs I write a telegram-bot. Api has a limit on the size of a single message - 4096 characters.
This is how I get the variable with the file search results:
with open('filename.list', 'r') as f: for line in f.readlines(): if searchstring.lower() in line.lower(): result= str(result) + str(line) + '\n' Example result:
String1 param1 param2 \ n String2 param1 param2 \ n
etc.
Further, this result is sent by the message:
if result: bot.send_message(chat_id="SomeID", text= str(result) ) How can I divide a message with the result into several messages, in case of exceeding the limit? At the same time, by doing this line by line, so that the results of a half-line are not transferred to me.
.readlines()here. The file in Python is already a row iterator. Do not usestr()on strings (and avoid explicitly calling at all) - this is useless. If not difficult, can you mention where you saw thefor line in f.readlines()construction (instead offor line in file)? What do you want to achieve by callingstr()? - jfs