import re
def title_except(s, exceptions):
word_list = re.split(' ', s) # re.split behaves as expected
final = [word_list[0].capitalize()]
for word in word_list[1:]:
final.append(word if word in exceptions else word.capitalize())
return " ".join(final)
articles = ['a', 'an', 'of', 'the', 'is']
print title_except('there is a way', articles)
# There is a Way
print title_except('a whim of an elephant', articles)
# A Whim of an Elephant
这有几个问题。如果使用split和join,一些空白字符将被忽略。内置的大写和标题方法不会忽略空格。
如果一个句子以一篇文章开头,你不希望标题的第一个字是小写的。
记住这些:
有以下方法:
没有小写项目选项。你必须自己编写代码,可能需要使用一个你想放低的文章列表。
使用titlecase.py模块!只对英语有效。
GitHub:https://github.com/ppannuto/python-titlecase
相关问题 更多 >
编程相关推荐