如何在Python字符串中替换括号和括号内的文本
我有两条像这样的线如何在Python字符串中替换括号和括号内的文本,python,regex,string,Python,Regex,String,我有两条像这样的线 string1 = "Today I went to the (market) to pick up some (fruit)." string2 = "Today I went to (school) to learn (algebra) and science." 我想根据下面的字典删除并替换两个字符串中的括号和括号内的文本 word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'a
string1 = "Today I went to the (market) to pick up some (fruit)."
string2 = "Today I went to (school) to learn (algebra) and science."
我想根据下面的字典删除并替换两个字符串中的括号和括号内的文本
word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'};
我希望新字符串为:
string1 = "Today I went to the library to pick up some books."
string2 = "Today I went to class to learn calculus and science."
为了做到这一点,我需要捕获括号内的文本,以便我可以使用它作为键来获取字典中的值。然后我需要用这个值替换括号和文本。我在正则表达式方面遇到了问题,以及如何执行此操作。您可以使用:
如果可以控制初始字符串使用
{}
而不是()
,则也可以使用:
如果您无法控制初始输出,但希望无论如何使用str.format()
,则可以将(
和)
的任何出现替换为{
和}
:
string1 = string1.replace('(', '{').replace(')', '}').format(**word_dict)
string2 = string2.replace('(', '{').replace(')', '}').format(**word_dict)
或者,要以更干净的方式执行此操作,您可以使用:
请记住,这将用花括号替换任何括号,即使它们没有围绕您要替换的单词。格式化字符串后,可以使用
rev_trd=str.maketrans('{}','()')
反向翻译剩余的花括号;但通常在这一点上,您最好只使用for
循环和str.replace()
,如第一个代码部分所示。除非您可以将初始字符串更改为仅包含花括号,否则请使用。您可以使用dict.items()
一种方法是将字符串拆分为单词和空格,然后搜索以
(
)开头的任何项目。在此之后,只需替换列表中的项目并将列表重新连接为字符串。例如
word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'};
string1 = "Today I went to the (market) to pick up some (fruit)."
string_list = string1.split()
for item in string_list:
if item[0] == '(':
i = string_list.index(item)
new_string = item[1:-1]
string_list[i] == word_dict[new_string]
print ' '.join(word[0] for word in string_list)
祝你好运,祝你编码愉快!看看这个,使用字典可能会起到作用:
原始字符串从哪里来?是否可能将所有的
(单词)
替换为{word}
?我觉得这应该是对我答案的评论。他问如何替换字符串中的内容,你的答案是“您可以使用dict.items()
”?我的意思是,这正是我的答案,只是有一个与问题无关的小改进,而且它对Python 2不起作用。@MarkusMeskanen我认为我的答案在性能方面会比您的好。
string1 = string1.replace('(', '{').replace(')', '}').format(**word_dict)
string2 = string2.replace('(', '{').replace(')', '}').format(**word_dict)
trd = str.maketrans('()', '{}')
string1 = string1.translate(trd).format(**word_dict)
string2 = string2.translate(trd).format(**word_dict)
>>> string1 = "Today I went to the (market) to pick up some (fruit)."
>>> string2 = "Today I went to (school) to learn (algebra) and science."
>>> word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'}
>>> for i,j in word_dict.items():
string1 = string1.replace('(' + i + ')', j)
string2 = string2.replace('(' + i + ')', j)
>>> string1
'Today I went to the library to pick up some books.'
>>> string2
'Today I went to class to learn calculus and science.'
word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'};
string1 = "Today I went to the (market) to pick up some (fruit)."
string_list = string1.split()
for item in string_list:
if item[0] == '(':
i = string_list.index(item)
new_string = item[1:-1]
string_list[i] == word_dict[new_string]
print ' '.join(word[0] for word in string_list)