简体   繁体   中英

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. Ie 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.

You can use regex for this;

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM