忽略区分大小写的关键字

2024-05-29 06:43:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图忽略大小写敏感时,搜索特定的子关键字,使用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集合中,我将如何搜索那些大小写相同的关键字(lower、upper、mixed)?你知道吗


Tags: toinidforcomment关键字replycomments
2条回答
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()添加到注释.正文在if语句中。你知道吗

使用str.lower()方法可以不区分大小写地比较两个字符串。例如

a = 'sTriNg LoWeR'
b = 'string lower'
c = 'STRING LOWER'

if(a.lower() == b.lower() == c.lower()):
    print('All equal!')

打印All equal!。你知道吗

相关问题 更多 >

    热门问题