Python 字符串操作:将每个句子的第一个字母大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22800401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:44:12  来源:igfitidea点击:

String manipulation: capitalize first letter of every sentence

pythonpython-3.x

提问by user3307366

I'm trying to write a program that capitalizes the first letter of each sentence.This is what I have so far, but I cannot figure out how to add back the period in between sentences.For example, if I input: hello. goodbye, the output is Hello Goodbye, and the period has disappeared.

我正在尝试编写一个将每个句子的第一个字母大写的程序。这是我目前所拥有的,但我不知道如何在句子之间添加句点。例如,如果我输入:hello。goodbye,输出Hello Goodbye,句号消失了。

string=input('Enter a sentence/sentences please:')
sentence=string.split('.')
for i in sentence:
    print(i.capitalize(),end='')

回答by Elias Zamaria

Maybe something like this:

也许是这样的:

print('.'.join(i.capitalize() for i in sentence))

回答by alvonellos

Okay, so my first answer was totally wrong. Here's another answer you can use, and it shows you some of the more powerful features of python, too. Suppose you have your string stored in s, where all your sentences are in a single string delimited by a comma. The following code returns that same exact string, separated by periods, but with the first characters of each sentence capitalized.

好的,所以我的第一个答案是完全错误的。这是您可以使用的另一个答案,它也向您展示了 python 的一些更强大的功能。假设您将字符串存储在 中s,其中所有句子都在一个以逗号分隔的字符串中。以下代码返回完全相同的字符串,由句点分隔,但每个句子的第一个字符大写。

'.'.join(map((lambda x: x[0].upper()+x[1:]), s.replace('. ','.').split('.')))

'.'.join(map((lambda x: x[0].upper()+x[1:]), s.replace('. ','.').split('.')))

Slick, right?

很滑吧?

回答by Nishant Nawarkhede

You can use,

您可以使用,

In [25]: st = "this is first sentence. this is second sentence. and this is third. this is fourth. and so on"

In [26]: '. '.join(list(map(lambda x: x.strip().capitalize(), st.split('.'))))
Out[26]: 'This is first sentence. This is second sentence. And this is third. This is fourth. And so on'

In [27]:

回答by Schickmeister

You just have to change one line:

你只需要改变一行:

string=input('Enter a sentence/sentences please:')
sentence=string.split('.')
for i in sentence:
    print (i.strip().capitalize()+". ",end='')

回答by Adam Wen

maybe you can do this:

也许你可以这样做:

string=input('Enter a sentence/sentences please:')
sentence='.'.join([i.capitalize() for i in string.split('.')])
print(sentence)

回答by jfs

You could use nltk for sentence segmentation:

您可以使用 nltk 进行句子分割

#!/usr/bin/env python3
import textwrap
from pprint import pprint
import nltk.data # $ pip install http://www.nltk.org/nltk3-alpha/nltk-3.0a3.tar.gz
# python -c "import nltk; nltk.download('punkt')"

sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
text = input('Enter a sentence/sentences please:')
print("\n" + textwrap.fill(text))
sentences = sent_tokenizer.tokenize(text)
sentences = [sent.capitalize() for sent in sentences]
pprint(sentences)

Output

输出

Enter a sentence/sentences please:
a period might occur inside a sentence e.g., see! and the sentence may
end without the dot!
['A period might occur inside a sentence e.g., see!',
 'And the sentence may end without the dot!']

回答by desired login

You could use regular expressions. Define a regex that matches the first word of a sentence:

您可以使用正则表达式。定义一个匹配句子第一个单词的正则表达式:

import re
p = re.compile(r'(?<=[\.\?!]\s)(\w+))

This regex contains a positive lookbehind assertion (?<=...)which matches either a ., ?or !, followed by a whitespace character \s. This is followed by a group that matches one or more alphanumeric characters \w+. In effect, matching the next word after the end of a sentence.

此正则表达式包含正向后断言(?<=...)它匹配或者是.?或者!,在一个空格字符\s。后面跟着一个匹配一个或多个字母数字字符的组\w+。实际上,匹配句子结束后的下一个单词。

You can define a function that will capitalise regex match objects, and feed this function to sub():

您可以定义一个将正则表达式匹配对象大写的函数,并将此函数提供给sub()

def cap(match):
    return(match.group().capitalize())

p.sub(cap, 'Your text here. this is fun! yay.')

You might want to do the same for another regex that matches the word at the beginning of a string:

您可能希望对另一个与字符串开头的单词匹配的正则表达式执行相同的操作:

p2 = re.compile(r'^\w+')

Or make the original regex even harder to read, by combining them:

或者通过组合它们使原始正则表达式更难阅读:

p = re.compile(r'((?<=[\.\?!]\s)(\w+)|(^\w+))')

回答by Stef II

This should work:

这应该有效:

import re
text = raw_input("Enter text: ")
rtn = re.split('([.!?] *)', text)
final = ''.join([i.capitalize() for i in rtn])
print final

回答by Saksham Varma

Try this:

尝试这个:

x = 'hello. how are you doing. nice to see. you'
print '.'.join(map(lambda x: x.title(), x.split('.')))

回答by Terry Jan Reedy

x = 'hello. goodbye. and how are you doing.'
print( '. '.join(map(lambda s: s.strip().capitalize(), x.split('.'))))

# Hello. Goodbye. And how are you doing.