Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除字符串中括号内的内容_Python_Regex_String - Fatal编程技术网

Python 删除字符串中括号内的内容

Python 删除字符串中括号内的内容,python,regex,string,Python,Regex,String,我想从字符串中删除括号和带有括号的内容。 我尝试了以下代码: a['Street Name'].str.replace('\(.*)','') 但它不起作用。谁能告诉我这句话有什么不对吗?试试这个: import re s = "I want to remove all words in brackets( like (this) and ((this)) and ((even) this))." while True: s_new = re.sub(r'\([^\(]*?\)',

我想从字符串中删除括号和带有括号的内容。 我尝试了以下代码:

a['Street Name'].str.replace('\(.*)','')
但它不起作用。谁能告诉我这句话有什么不对吗?

试试这个:

import re

s = "I want to remove all words in brackets( like (this) and ((this)) and ((even) this))."

while True:
    s_new = re.sub(r'\([^\(]*?\)', r'', s)
    if s_new == s:
        break
    s = s_new

print(s_new) # I want to remove all words in brackets.

你说“不工作”是什么意思?[街道名称]的内容是什么?你得到了什么结果?你希望得到什么结果?需要
a['Street Name']=a['Street Name'].str.replace(r'\([^]*\),'')
@Maroun Maroun a['Street Name']的内容是“Dharwad(hubbali Dharwad avali Nagar)”。我希望输出为“Dharwad”@bigbind,['Street Name']的内容是“Dharwad(Hubbali Dharwad Avali Nagara)。我希望输出为“Dharwad”,没有括号将使用此代码删除所有内容