如何替换单个字符而不影响该字符在整个单词中的出现?

2024-05-14 14:01:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我从PDF中提取了单词,我注意到要点被转换为o

原文:

enter image description here

提取的文本(o不是从行的开头开始,我只是为了可读性而格式化了它):

o Organize and coordinate ad hoc task forces for specific topics based on demand from AFR100 countries o Keep document repository for AFR100 including documentation of focal points, pledge and affiliation documents, progress information from countries, etc. o Treatment of demands from countries and identification of potential technical partners to assist together with the support of the MT  

所以我想知道,只有当o不在一个单词中时,我如何删除它


Tags: andofthefrom文本coordinateforpdf
1条回答
网友
1楼 · 发布于 2024-05-14 14:01:01

您可以使用此正则表达式:

txt = '''
o Organize and coordinate ad hoc task forces for specific topics based on demand from AFR100 countries
o Keep document repository for AFR100 including documentation of focal points, pledge and affiliation documents, progress information from countries, etc.
o Treatment of demands from countries and identification of potential technical partners to assist together with the support of the MT
'''

import re

txt = re.sub(r'\bo\b', '', txt)

print(txt)

输出:

 Organize and coordinate ad hoc task forces for specific topics based on demand from AFR100 countries
 Keep document repository for AFR100 including documentation of focal points, pledge and affiliation documents, progress information from countries, etc.
 Treatment of demands from countries and identification of potential technical partners to assist together with the support of the MT

正则表达式中的\b是一个单词边界,因此,请查找两边各有一个单词边界的o,即由一个o的出现组成的单词

相关问题 更多 >

    热门问题