2010-09-16 24 views
62

在Python中是否有標準方式來標定一個字符串(即,單詞以大寫字符開頭,所有剩餘的裝入字符都是小寫字母),但是仍然放置像and,inof這樣的文章?字符串有例外

回答

116

這有幾個問題。如果您使用拆分和連接,則一些空格字符將被忽略。內置的大寫和標題方法不會忽略空格。

>>> 'There  is a way'.title() 
'There  Is A Way' 

如果一個句子從一篇文章開始,您不希望小寫的第一個單詞是小寫。

記住這些:

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 
+5

+1爲「以文章開頭的句子」案例 – yassin 2010-09-16 19:36:44

+0

爲什麼'重新'是必要的?有一個''「」.split'函數可以做到這一點。 – wizzwizz4 2017-03-19 18:41:29

+0

@ wizzwizz4:'str.split'不考慮連續的空格。 're.split'保留空格。所以,這個函數不會佔用任何空間。 – dheerosaur 2017-03-19 20:01:06

14

有以下方法:

>>> mytext = u'i am a foobar bazbar' 
>>> print mytext.capitalize() 
I am a foobar bazbar 
>>> print mytext.title() 
I Am A Foobar Bazbar 

有沒有小寫文章選項。您必須自己編寫代碼,可能需要使用要降低的文章列表。

+0

titlecase.py小寫文章。 – 2012-12-14 02:03:22

3
capitalize (word) 

這應該做。我有不同的看法。

>>> mytext = u'i am a foobar bazbar' 
>>> mytext.capitalize() 
u'I am a foobar bazbar' 
>>> 

OK作爲答覆說上面的,你必須做出一個自定義的大寫:

mytext的= u'i是一個foobar的bazbar」

def xcaptilize(word): 
    skipList = ['a', 'an', 'the', 'am'] 
    if word not in skipList: 
     return word.capitalize() 
    return word 

k = mytext.split(" ") 
l = map(xcaptilize, k) 
print " ".join(l) 

此輸出

I am a Foobar Bazbar 
+0

這不是我想要的。我想得到「我是Foobar Bazbar」 – yassin 2010-09-16 16:53:15

+0

@ Yassin Ezbakhe:編輯我的回答,這應該適合你。文章列表可以很容易地從任何字典中提取 – pyfunc 2010-09-16 17:12:29

1
not_these = ['a','the', 'of'] 
thestring = 'the secret of a disappointed programmer' 
print ' '.join(word 
       if word in not_these 
       else word.title() 
       for word in thestring.capitalize().split(' ')) 
"""Output: 
The Secret of a Disappointed Programmer 
""" 

標題始於帽子意大利文字,與文章不符。

40

使用titlecase.py模塊!僅適用於英語。

>>> from titlecase import titlecase 
>>> titlecase('i am a foobar bazbar') 
'I Am a Foobar Bazbar' 
+1

如果您要轉換的字符串包含任何位置的數字,則標題模塊不起作用。 – Troy 2013-07-24 23:57:06

+1

@Troy似乎數字問題是固定的,或者我沒有觸及你的邊緣情況。例如:titlecase('one 4 two') - >'One 4 Two'。現在是Titlecase('1one') - >'1one',但'1one'.title() - >'1One'。儘管後面的案例是一個邊緣案例,我不確定'1One'是否是正確的標題。我也不太關心抓住我的語法書。 – 2014-09-22 04:53:30

+0

在「321 A BROADWAY STREET」的情況下,我得到「321百老匯街」無效。使用上述dheerosaur提出的解決方案產生「百老匯街321號」。 – MoreScratch 2016-10-28 20:49:03

9

a Perl script written by John GruberStuart Colville has made a Python port爲字符串到標題情況轉換,而是基於從風格紐約時報手冊,以及對幾種特殊情況的餐飲規則,避免資本小言。

一些腳本的聰明:

  • 他們大寫像小的話,如果在的,上等,而將未利用他們,如果他們錯誤地資本化輸入。

  • 腳本假定帶有第一個字符以外的大寫字母的單詞已經正確大寫。這意味着他們只會留下一個像「iTunes」這樣的詞,而不是將其改寫成「iTunes」,或者更糟糕的是,「Itunes」。

  • 它們跳過任何帶有點線的單詞; 「example.com」和「del.icio.us」將保持小寫。

  • 它們具有硬編碼的黑客專管奇數的情況下,像「AT & T」和「Q & A」,這兩者都含有少量的單詞(在和),其通常應爲小寫。

  • 標題的第一個也是最後一個單詞總是大寫,因此輸入例如「Nothing to be afraid of」將變成「Nothing to Be afraid」。

  • 冒號後的一個小詞將會大寫。

您可以下載它here

3

Python 2.7的標題方法有一個缺陷。

value.title() 

將返回匠「小號助理當值匠」 小號助理

最好的解決辦法是可能使用首字母大寫從斯圖爾特科爾維爾從@BioGeek之一。這與@Etienne提出的解決方案是相同的。

0

使用列表中理解和三元運算符一襯墊

reslt = " ".join([word.title() if word not in "the a on in of an" else word for word in "Wow, a python one liner for titles".split(" ")]) 
print(reslt) 

擊穿:

for word in "Wow, a python one liner for titles".split(" ")拆分串入一個列表,並啓動一個for循環(在列表comprehenstion)

word.title() if word not in "the a on in of an" else word使用本機方法title()標題大小寫字符串,如果它不是文章

" ".join用分隔符(空格)加入列表元素