質問

これは良いです。

  
    
      

輸入列       string.capwords( "適切な名前")       '適切な名前'

    
  

これはあまりよくないです。

  
    
      

string.capwords( "I.R.S")       'I.r.s'

    
  

それは頭文字をaccomodatesようcapwordsを行うには、文字列の方法はありますか?

役に立ちましたか?

解決

これは動作します。

import re

def _callback(match):
    """ This is a simple callback function for the regular expression which is 
        in charge of doing the actual capitalization. It is designed to only 
        capitalize words which aren't fully uppercased (like acronyms).
    """
    word = match.group(0)
    if word == word.upper():
        return word
    else:
        return word.capitalize()

def capwords(data):
    """ This function converts `data` into a capitalized version of itself. This 
        function accomidates acronyms.
    """
    return re.sub("[\w\'\-\_]+", _callback, data)

ここでテストされます:

print capwords("This is an IRS test.")    # Produces: "This Is An IRS Test."
print capwords("This is an I.R.S. test.") # Produces: "This Is An I.R.S. Test."

他のヒント

いいえ、標準ライブラリには、このような方法はありません。

そのような機能があったとしても、「IRS」を処理するように求められたときに、

、それは何をしますか?でも、IRSは、ドットなしで「IRS」を名乗るます。

私は、リストの理解を用いる[original_list内のエントリの(entry.split()]中のLは[string.capwords(L)を " ")に参加します"。"]

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top