python - 如何在 Python 中搜索字符串的所有大写和小写版本?

标签 python twitter

我正在学习如何通过 tweepy 使用 Twitter API。我制作了一个最近的推文列表,其中包含关键字“茶园”。我想从此列表中搜索包含这些字符串的所有大写变体的推文中的字符串。

我的带有关键字的初始推文列表

public_tweets = api.search("tea garden", count=200)

我要在此代码中添加什么来搜索字符串的所有大写/小写变体?在此示例中,它会将推文添加到包含“Garden Tea”或“GARDEN TEA”的列表中。

list = []

for tweet in public_tweets:
  if "garden tea" in tweet.text:
    list.append(tweet)

最佳答案

可以先用str.lower转成小写,再比较:

list = []

for tweet in public_tweets:
  if "garden tea" in tweet.text.lower():
    list.append(tweet)

关于python - 如何在 Python 中搜索字符串的所有大写和小写版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016430/

相关文章:

android - 推特可以上传照片吗?如果是,那么如何从 Android 发布或上传照片?

scala - 无法使用 Spark 流应用程序查看 Twitter 流

python - 在代理后面使用 Jupyter

python - App Engine 将项目附加到 ListProperty

Python.Net 无法导入 dll

ruby-on-rails - 如何实现 twitter-text-rb

twitter - 正在搜索带有 Unicode 字符/Apple 表情符号的推文?

python - 如何在Python中创建多个类实例?

Python 和 Statsmodels : How to include the alternative test_t hypothesis?

javascript - 如何创建类似 facebook/twitter 的按钮小部件