2016-11-15 38 views
2

这里是我的代码:善用每个句子的第一个字母在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没有被资本化,因为我劈在.和这句话有一个以上这段时间后的空间,但我不知道我该如何解决这个问题,或者如果有更好的方式去做我正在做的事情。任何帮助将不胜感激。

+1

你想之前,“也许”保留4空间或者更改标准1个空间? – Skycc

+0

@Skycc我想保留4个空格。刚刚通过某人的帮助编辑了我的问题,所以更清楚。 – Kblo55

+0

http://stackoverflow.com/questions/26320697/capitalization-of-each-sentence-in-a-string-in-python-3 –

回答

0

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

+0

我试图做到这一点,但无法让它工作。你能解释一下,如果你不介意,我会怎么做? – Kblo55

+1

http://stackoverflow.com/questions/2378962/returning-the-lowest-index-for-the-first-non-whitespace-character-in-a-string-in –

+0

帮助我找到索引,但第一部分我无法弄清楚如何用大写版本替换索引。 – Kblo55

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 
相关问题