最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

【已解决】python中判断单个或多个单词是否是全部小写或首字母小写

Python crifan 149浏览 0评论

折腾:

【已解决】把文本格式的剧本内容用Python批量导入后台系统

期间,需要去判断一些topic:

Classroom layout

routine

Food

vegetable

是否存在于数据库中,而数据库中的名字和此处的topic,可能是完全一样

可能是存在首字母大小写不同

比如:

是:

classroom layout

需要去判断出来

python 首字母大写

【总结】

参考:

Python天天美味(11) – 可爱的大小写 – CoderZh – 博客园

<code>s = 'hEllo pYthon'
print s.upper() # HELLO PYTHON
print s.lower() # hello python
print s.capitalize() # Hello python
print s.title() # Hello Python
</code>

然后自己去用试试:

<code>
def findRealTopic(curTopic, topicList=[]):
  isExisted, realTopicName = (False, "")

  originTopic = curTopic
  allLowcaseTopic = curTopic.lower()
  capitalizedTopic = curTopic.capitalize()
  titledTopic = curTopic.title()
  logging.info("originTopic=%s,allLowcaseTopic=%s,capitalizedTopic=%s,titledTopic=%s",
    originTopic, allLowcaseTopic, capitalizedTopic, titledTopic)

  if originTopic in topicList:
    isExisted = True
    realTopicName = originTopic
  elif allLowcaseTopic in topicList:
    isExisted = True
    realTopicName = allLowcaseTopic
  elif capitalizedTopic in topicList:
    isExisted = True
    realTopicName = capitalizedTopic
  elif titledTopic in topicList:
    isExisted = True
    realTopicName = titledTopic
  else:
    isExisted = False
    realTopicName = ""
    logging.error("Not found topic: %s", curTopic)

  logging.info("topic: %s -&gt; isExisted=%s, realTopicName=%s", curTopic, isExisted, realTopicName)
  return isExisted, realTopicName
</code>

输出:

<code>topic: Hobbies -&gt; isExisted=True, realTopicName=Hobbies
topic: Leisure -&gt; isExisted=True, realTopicName=leisure
topic: family -&gt; isExisted=True, realTopicName=Family
topic: Classroom layout -&gt; isExisted=True, realTopicName=Classroom Layout
</code>

转载请注明:在路上 » 【已解决】python中判断单个或多个单词是否是全部小写或首字母小写

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
59 queries in 0.081 seconds, using 9.34MB memory