在python字符串中用'@'替换'(a)'

Joh*_*ger -2 python regex string replace

我正在遍历电子邮件地址列表,发现输入的数字不正确,即(a)而不是@.所以,我试图用@取代(a).到目前为止我得到的最好的是:

x = 'asdf(a)asdf.com'
found = re.sub(r'\s(a)\s', '@', x.strip(), flags=re.IGNORECASE)
print(found)
Run Code Online (Sandbox Code Playgroud)

但是,这只是打印原始输入:

asdf(a)asdf.com
Run Code Online (Sandbox Code Playgroud)

我尝试了一些正则表达式,但它不起作用.请帮忙!

所有建议使用str.replace()方法的人的注释.由于(a)是字符串的一部分,我必须将整个文档作为(列表)字符串读取,然后迭代它.我不认为这在处理能力方面是一种经济的解决方案.此外,问题特别要求正则表达式而不是字符串方法.感谢您抽出宝贵时间作出回应!

And*_*ndy 9

这似乎很容易使用 .replace()

>>> x = 'asdf(a)asdf.com'
>>> x.replace('(a)', '@')
'asdf@asdf.com'
Run Code Online (Sandbox Code Playgroud)