【问题标题】:Preserve paragraph marks while capitalzing - RegEx大写时保留段落标记 - RegEx
【发布时间】:2018-09-09 03:08:44
【问题描述】:
p = re.compile(r'((?<=[\.\?!]\s)(\w+)|(^\w+))')
def cap(match):
    return(match.group().capitalize())
capitalized_1 = p.sub(cap, Inputfile)

with codecs.open('o.txt', mode="w", encoding="utf_8") as file:
  file.write(capitalized_1)

我正在使用正则表达式将 . 之后的字母大写。 ? !上面的代码就是这样做的。但它会去掉段落标记(分页符)并将其合并为一个大段落。

如何保留段落标记并防止结块?

输入文件:

在插入选项卡上,画廊包含旨在与文档的整体外观相协调的项目。您可以使用这些库插入表格、页眉、页脚、列表、封面和其他文档构建块。当您创建图片、图表或图表时,它们还会与您当前的文档外观相协调。

您可以通过从主页选项卡上的快速样式库中选择所选文本的外观来轻松更改文档文本中所选文本的格式。您还可以使用主页选项卡上的其他控件直接格式化文本。大多数控件都提供了使用当前主题外观或使用您直接指定的格式的选择。

电流输出

在插入选项卡上,画廊包括旨在与文档的整体外观相协调的项目。您可以使用这些库来插入表格、页眉、页脚、列表、封面和其他文档构建块。当您创建图片、图表或图表时,它们也会与您当前的文档外观相协调。通过从主页选项卡上的快速样式库中选择所选文本的外观,您可以轻松更改文档文本中所选文本的格式。您还可以使用主页选项卡上的其他控件直接设置文本格式。大多数控件都提供了使用当前主题外观或使用您直接指定的格式的选择。

预期输出:

在插入选项卡上,画廊包括旨在与文档的整体外观相协调的项目。您可以使用这些库来插入表格、页眉、页脚、列表、封面和其他文档构建块。当您创建图片、图表或图表时,它们还会与您当前的文档外观相协调。

通过从主页选项卡上的快速样式库中选择所选文本的外观,您可以轻松更改文档文本中所选文本的格式。您还可以使用主页选项卡上的其他控件直接设置文本格式。大多数控件都提供了使用当前主题外观或使用您直接指定的格式的选择。

编辑 1:

import re,codecs
def capitalize(match):
    return ''.join([match.group(1), match.group(2).capitalize()])

with codecs.open('i.txt', encoding='utf-8') as f:
    text = f.read()
    
pattern = re.compile('(^|[.?!]\s+)(\w+)?')

print(pattern.sub(capitalize, text))

当我尝试根据答案 1 方法从文件中读取它时引发错误。

return ''.join([match.group(1), match.group(2).capitalize()])
AttributeError: 'NoneType' object has no attribute 'capitalize'

【问题讨论】:

  • 明白了。只需将块放在引号中以使其清楚。
  • 是否可以选择逐行拆分文件,还是出于某种原因您必须这样做是正则表达式?

标签: python regex capitalization


【解决方案1】:

你可以这样做:

import re


def capitalize(match):
    return ''.join([match.group(1), match.group(2).capitalize()])

text = """on the insert tab, the galleries include items that are designed to coordinate with the overall look of your document. you can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. when you create pictures, charts, or diagrams, they also coordinate with your current document look.

you can easily change the formatting of selected text in the document text by choosing a look for the selected text from the quick styles gallery on the home tab. you can also format text directly by using the other controls on the home tab. most controls offer a choice of using the look from the current theme or using a format that you specify directly."""

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

print(pattern.sub(capitalize, text))

输出

On the insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.

You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the quick styles gallery on the home tab. You can also format text directly by using the other controls on the home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.

备注

  • (^|[.?!]\s+) 表示捕获.(点)、?! 后跟一个或多个空格字符(制表符、空格等)。 ^ 表示字符串的开头;所以这个组的完整含义是句子的开头或.?!,后跟一个空格。
  • (\w+)? 表示一个或多个单词字符
  • 然后大写函数保留第一组匹配的内容并将第二组(单词)大写。

【讨论】:

  • 你测试过相同的文本吗?
  • 是的,但将其保存在文本文件中并尝试从那里读取。
猜你喜欢
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
相关资源
最近更新 更多