使用正则表达式PYTHON替换文件中的特定字符串

使用正则表达式PYTHON替换文件中的特定字符串,python,regex,Python,Regex,我正在使用Stanford NER标记一个文件,我想用“无”替换每个“O”标记。我已经尝试过这段代码,但它显示了错误的输出。问题是它会替换字符串中的每个“O”。我不熟悉正则表达式,也不知道什么是适合我的问题的正则表达式。蒂亚 这是我的密码: 样本输入: 输出: 您不需要循环使用string\u type,直接在字符串上使用re.sub,应该可以: s = """Tropical O Storm O Jolina O affects O 2,000 O pe

我正在使用Stanford NER标记一个文件,我想用“无”替换每个“O”标记。我已经尝试过这段代码,但它显示了错误的输出。问题是它会替换字符串中的每个“O”。我不熟悉正则表达式,也不知道什么是适合我的问题的正则表达式。蒂亚

这是我的密码:

样本输入:

输出:


您不需要循环使用
string\u type
,直接在字符串上使用
re.sub
,应该可以:

s = """Tropical O
    Storm O
    Jolina O
    affects O
    2,000 O
    people O
    MANILA LOCATION
    , O
    Philippines LOCATION
    – O
    Initial O
    reports O
    from O
    the O"""

import re
print(re.sub(r"\bO(?=\n|$)", "NONE", s))
给出:

Tropical NONE
    Storm NONE
    Jolina NONE
    affects NONE
    2,000 NONE
    people NONE
    MANILA LOCATION
    , NONE
    Philippines LOCATION
    – NONE
    Initial NONE
    reports NONE
    from NONE
    the NONE

这里的
\bO(?=\n |$)
匹配单个字母
O
,后跟新行字符
\n
或行尾
$

什么是
字符串类型
?您似乎在循环一个字符串,该字符串将逐个字符进行检查。@Psidom我将带标签的文本(元组)转换为字符串(字符串类型),然后逐行读取。在什么情况下失败。例如,我试着像
line='TrOpical O're.sub('O$,'NONE',line)
'TrOpical NONE'
Tropical NONE
Storm NONE
Jolina NONE
affects NONE
2,000 NONE
people NONE
MANILA LNONECATINONEN
, NONE
Philippines LNONECATINONEN
– NONE
Initial NONE
reports NONE
from NONE
the NONE
s = """Tropical O
    Storm O
    Jolina O
    affects O
    2,000 O
    people O
    MANILA LOCATION
    , O
    Philippines LOCATION
    – O
    Initial O
    reports O
    from O
    the O"""

import re
print(re.sub(r"\bO(?=\n|$)", "NONE", s))
Tropical NONE
    Storm NONE
    Jolina NONE
    affects NONE
    2,000 NONE
    people NONE
    MANILA LOCATION
    , NONE
    Philippines LOCATION
    – NONE
    Initial NONE
    reports NONE
    from NONE
    the NONE