Python 忽略区分大小写的关键字集
我试图在使用PRAW搜索特定子Reddits上的关键字时忽略大小写敏感度Python 忽略区分大小写的关键字集,python,api,reddit,praw,Python,Api,Reddit,Praw,我试图在使用PRAW搜索特定子Reddits上的关键字时忽略大小写敏感度 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('tes
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
集合中,如何搜索任何大小写(小写、大写、混合)的相同关键字?使用str.lower()
方法,您可以不区分大小写地比较两个字符串。比如说
a = 'sTriNg LoWeR'
b = 'string lower'
c = 'STRING LOWER'
if(a.lower() == b.lower() == c.lower()):
print('All equal!')
打印<代码>全部相等代码>
if keyword.lower() in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
到
您可以在if语句中添加一个.lower()到comment.body。为什么不能只“lower()”注释body
if keyword.lower() in comment.body.lower() and comment.id not in comments_replied_to and comment.author != r.user.me():