'。','!'之后的大写字母和'?'用Python签名

时间:2017-01-15 14:05:16

标签: python uppercase

我一直在搜索Stack Overflow,但无法找到正确的代码来纠正,例如。

bibliographystyle{apa}

分为:

\begin{document}

The body of the document goes here...

\newpage

\bibliography{bibliography} % Or whatever you decided to call your .bib file 

\usepackage[round, comma, sort&compress ]{natbib} 

bibliographystyle{apa}
\end{document}

4 个答案:

答案 0 :(得分:2)

  1. 使用正则表达式以标点字符分割
  2. 在句子开头用大写字母加入所有内容。
  3. 例如:

    import re
    text = 'hello! are you tired? no, not at all!'
    
    punc_filter = re.compile('([.!?]\s*)')
    split_with_punctuation = punc_filter.split(text)
    
    final = ''.join([i.capitalize() for i in split_with_punctuation])
    print(final)
    

    输出:

    >>> Hello! Are you tired? No, not at all!
    

答案 1 :(得分:2)

您可以尝试这种正则表达式方法:

import re
re.sub("(^|[.?!])\s*([a-zA-Z])", lambda p: p.group(0).upper(), s)
# 'Hello! Are you tired? No, not at all!'
  • (^|[.?!])匹配字符串^.?!的开头,后跟可选空格;
  • [a-zA-Z]在第一个模式后直接匹配字母;
  • 使用lambda函数将捕获的组转换为大写;

答案 2 :(得分:0)

您可以执行类似

的操作
x = "hello! are you tired? no, not at all!"
y = x.split("? ")
z="" 
for line in y:
    z = "{} {}".format(z,line.capitalize())
print(z)

根据您的需要进行调整。

答案 3 :(得分:0)

capitalize()方法使除第一个字母外的所有字母变小。

更一般的变体是:

def capitalize(text):
    punc_filter = re.compile('([.!?;]\s*)')
    split_with_punctuation = punc_filter.split(text)
    for i,j in enumerate(split_with_punctuation):
        if len(j) > 1:
            split_with_punctuation[i] = j[0].upper() + j[1:]
    text = ''.join(split_with_punctuation)
    return text

text = "hello Bob! are you tired? no, not at all!"
capitalize(text)

输出:

'Hello Bob! Are you tired? No, not at all!'
相关问题