如何在 Python 中为字符串添加缺少的右括号? [英] How to add a missing closing parenthesis to a string in Python?

查看:89
本文介绍了如何在 Python 中为字符串添加缺少的右括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个字符串要进行后期处理,其中很多首字母缩略词都缺少右括号.假设下面的字符串text,但也假设这种类型的缺失括号经常发生.

I have multiple strings to postprocess, where a lot of the acronyms have a missing closing bracket. Assume the string text below, but also assume that this type of missing bracket happens often.

我下面的代码仅通过将右括号独立添加到缺少的首字母缩略词中而起作用,而不是添加到完整的字符串/句子中.关于如何有效地执行此操作的任何提示,最好不需要迭代?

My code below only works by adding the closing bracket to the missing acronym independently, but not to the full string/sentence. Any tips on how to do this efficiently, and preferably without needing to iterate ?

import re
 
#original string
text = "The dog walked (ABC in the park"

#Desired output:
desired_output = "The dog walked (ABC) in the park"


#My code: 
acronyms = re.findall(r'\([A-Z]*\)?', text)
for acronym in acronyms:
  if ')' not in acronym: #find those without a closing bracket ')'. 
    print(acronym + ')') #add the closing bracket ')'.

#current output:
>>'(ABC)'

推荐答案

您可以使用

text = re.sub(r'(\([A-Z]+(?!\))\b)', r"\1)", text)

使用这种方法,您还可以摆脱之前文本是否包含 ) 的检查,请参阅 regex101.com 上的演示.

With this approach, you can also get rid of the check if the text has ) in it before, see a demo on regex101.com.

全文:

import re
 
#original string
text = "The dog walked (ABC in the park"
text = re.sub(r'(\([A-Z]+(?!\))\b)', r"\1)", text)
print(text)

这产生了

The dog walked (ABC) in the park

请参阅 ideone.com 上的工作演示.

这篇关于如何在 Python 中为字符串添加缺少的右括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆