在Python中将每个句子的第一个字母大写

时间:2016-11-15 23:31:19

标签: python

这是我的代码:

def fix_capitalization(usrStr):
    newString = ''
    wordList = []
    numLetters = 0
    for s in usrStr.split('. '):
        if s[0].isupper():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(s)
        if s.islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(s)
            numLetters += 1

        if s[0].islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(s)
            numLetters += 1 



    newString = '. '.join(wordList)
    return newString, numLetters

传入的字符串是:

i want some water. he has some. maybe he can give me some. i think I will ask.

请注意maybe之前有4个空格。我想要的结果是:

I want some water. He has some. Maybe he can give me some. I think I will ask.

但我明白了:

I want some water. He has some. maybe he can give me some. I think I will ask.

我知道maybe没有被大写,因为我在.分裂并且句子在句号之后有多个空格,但我不知道我怎么能解决这个问题,或者是否有更好的方式去做我正在做的事情。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

for for循环: 首先找到非空格字符的索引。 然后用s [index]替换s [0]。

答案 1 :(得分:0)

使用正则表达式子方法的解决方案

def fix_capitalization(mystr):
    numLettets = 0
    newstr = []
    for s in mystr.split('. '):
        tmp = re.sub('^(\s*\w+)', lambda x:x.group(1).title(), s)
        newstr.append(tmp)
        # num of letters
        if s.lstrip()[0] != tmp.lstrip()[0]:
            numLetters += 1          
    return '. '.join(newstr).replace(' i ', ' I '), numLetters

fix_capitalization( 'i want some water. he has some.    maybe he can give me some. i think I will ask.')
# return ('I want some water. He has some.    Maybe he can give me some. I think I will ask.', 4)

简单修复原始代码,如下所示

def fix_capitalization(usrStr):
    newString = ''
    wordList = []
    numLetters = 0
    for s in usrStr.split('. '):
        # check for leading space
        lspace = len(s) - len(s.lstrip())
        s = s.lstrip()

        if s[0].isupper():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(' '*lspace + s)
        if s.islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(' '*lspace + s)
            numLetters += 1
        if s[0].islower():
            s = s.capitalize()
            s = s.replace(' i ', " I ") 
            wordList.append(' '*lspace + s)
            numLetters += 1 

    newString = '. '.join(wordList)
    return newString, numLetters