E
E
EVG822021-12-25 20:41:26
Python
EVG82, 2021-12-25 20:41:26

How to read from a file?

I got such an implementation
https://qna.habr.com/q/842185

now the task. so that there is a separate file where the words go in a list, to which the blacklist command
here I have a plug.
It is clear that I am doing everything wrong. but how should it be? where to read? What should I add to this code to make it work? Useful, I think it will be all

How to read from a file? Python
Help me figure it out or throw a lesson where about it?
in the main lesson how to read and display. I don't need a conclusion.
I need the word to be taken from the file and substituted into the blacklist construction
There are about 1000 words to block.
So the search must be made fast

I think that you need to write something blacklist=[' '] here, but I can understand the line-by-line principle of
inputfile operation - we turned to the
open file, opened it and read
further what?
how to make it so that if the word in the blacklist was removed from the message or the entire message?

inputfile = '1.txt'
open(inputfile, mode='r', encoding='utf-8')
blacklist=[' what?']

@bot.message_handler(content_types=["text"])
def handle_text( message):
for x in blacklist:
if (x in message.text):
bot.delete_message(message.chat.id, message.message_id)
else:
pass

if __name__ == "__main__":
bot.infinity_polling()

Please help me understand how this all works line by line

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
desaki, 2021-12-25
@desaki

There are many answers to this question on the Internet, I will try to explain it very simply ..
You have a blacklist.txt file:

with open(f'blacklist.txt', encoding = "utf-8") as f:
  lines = f.read()
if message.text.lower() in lines:
  print("Слово из ЧС")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question