Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How remove the text between curly bracket

I have strings extracted from a csv file. I want to know how to remove the text between the curly brackets from the string using Python, ex:

string = 'some text hear { bracket } some text here'

I want to get:

some text hear some text here

I hope anyone can help me to solve this problem, thank you.

EDIT: answer

import re
string = 'some text hear { bracket } some text here'
string = re.sub(r"\s*{.*}\s*", " ", string)
print(string)
like image 937
Indrajith Jayasooriya Avatar asked Mar 13 '26 11:03

Indrajith Jayasooriya


1 Answers

Given:

>>> s='some text here { bracket } some text there'

You can use str.partition and str.split:

>>> parts=s.partition(' {')
>>> parts[0]+parts[2].rsplit('}',1)[1]
'some text here some text there'

Or just partition:

>>> p1,p2=s.partition(' {'),s.rpartition('}')
>>> p1[0]+p2[2]
'some text hear some text there'

If you want a regex:

>>> re.sub(r' {[^}]*}','',s)
'some text hear some text there'
like image 147
dawg Avatar answered Mar 15 '26 04:03

dawg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!