Игнорировать набор ключевых слов с учетом регистра
Я пытаюсь игнорировать чувствительность к регистру при поиске ключевых слов в определенных субредитах, используя PRAW.
def run_bot(r, comments_replied_to):
print "Obtaining 25 comments..."
keywords = {"eyebleach", "eye bleach", "enough internet for today", "enough internet for the day"}
for comment in r.subreddit('test').comments(limit=25):
for keyword in keywords:
if keyword.lower() in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
print "Keyword found in comment " + comment.id + "!"
posts = r.subreddit('eyebleach').random()
print("Generated random image post from /r/eyebleach: " + posts.url)
comment_reply = "[**Need some eye bleach?**](%s)" % posts.url
comment_reply += "\n\n/u/eyebleacher_bot was created by [@cjgetty](http://github.com/cjgetty).\n\nThis eye bleach has been randomly generated from [/r/eyebleach](http://reddit.com/r/eyebleach)."
comment.reply(comment_reply)
print "Replied to comment " + comment.id + "!"
comments_replied_to.append(comment.id)
with open ("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
print "Sleeping for 10 seconds..."
#Sleep for 10 seconds...
time.sleep(10)
в keywords
установить, как мне искать эти же ключевые слова в любом случае (нижний, верхний, смешанный)?
1 ответ
if keyword.lower() in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
к
if keyword.lower() in comment.body.lower() and comment.id not in comments_replied_to and comment.author != r.user.me():
Вы добавляете .lower() к comment.body в свой оператор if.
С использованием str.lower()
Метод, который вы можете без учета регистра сравнить две строки. Например
a = 'sTriNg LoWeR'
b = 'string lower'
c = 'STRING LOWER'
if(a.lower() == b.lower() == c.lower()):
print('All equal!')
печать All equal!
,