Stefan
Stefan

Reputation: 69

remove content between a specific character and whitespace

I have a string in python of floats that looks like this:

"a b c,d e"

And I would like to efficiently change it to this:

"a b c e"

The original string is a line in a text file and there may be more than one comma in the string. I.e. every time there is a comma I want to remove all characters between it and whitespace. I could do this with a for loop, make a list, and then join that list with a whitespace character, but I need to do this many times so speed is important. Hopefully using regular expressions.

Just to be clear a,b,c,d and e are not characters they usually look like 1.00e-03 or something.

Upvotes: 0

Views: 96

Answers (3)

Kasravnd
Kasravnd

Reputation: 107287

If you need more speed as a simplest way you can use split :

>>> s="a b c,d e r ,y g k"
>>> sp=s.split(',')
>>> sp
['a b c', 'd e r ', 'y g k']
>>> new=[i[1:].strip() for i in sp[1:]]
>>> new
['e r', 'g k']
>>> new_string=sp[0]+' '+' '.join(new)
>>> new_string
'a b c e r g k'

Upvotes: 0

Martol1ni
Martol1ni

Reputation: 4702

text = "a b c,d e"
re.sub(re.search(r'(,\w)', text).group(1), '', text)
>> "a b c e"

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

You can use regex for this;

>>> import re
>>> s = "a b c,d e"
>>> re.sub(r',\S+', '', s)
'a b c e'

Upvotes: 1

Related Questions