replacing multiple instances of commas beginning at specific position

Bengt Richter bokr at
Wed Nov 16 00:49:52 CET 2005

On Tue, 15 Nov 2005 08:26:22 GMT, Dennis Lee Bieber <wlfraed at> wrote:

>On 14 Nov 2005 09:43:57 -0800, "striker" <striker at> declaimed
>the following in comp.lang.python:
>> What would be the best approach to replace all instances of multiple
>> commas with just one comma, except for the 4 commas prior to ADMNSRC?
>	Simplify the problem... Start by rephrasing... Since ADMNSRC is to
>always have four commas leading up to it (at least, as I understand your
>statement of intent), you could consider three of those to be part of
>the string. So...
>	Phase one: split on commas, tossing out any null fields
>	Phase two: replace "ADMNSRC" with ",,,ADMNSRC"
>	Phase three: rejoin the parts that remain.
>	Doing this efficiently may be another matter but...
>data = [ "one, two, three,,,,four,,,,ADMNSRC, five,,,,six",
>         "one, two, three,four,,,,,,,,ADMNSRC,,,,,,eighteen,   and so
>on" ]
>result = []
>for ln in data:
>    wds = [x.strip() for x in ln.split(",") if x]
>    for i in range(len(wds)):
>        if wds[i] == "ADMNSRC":
>            wds[i] = ",,,ADMNSRC"
>    result.append(",".join(wds))
>print result

Or if data is from a single file read, maybe (untested beyond what you see ;-)

 >>> data = """\
 ... one, two, three,,,,four,,,,ADMNSRC, five,,,,six
 ... one, two, three,four,,,,,,,,ADMNSRC,,,,,,eighteen,   and so on
 ... """
 >>> import re
 >>> rxc = re.compile(',+')
 >>> result = ',,,ADMNSRC'.join(','.join(rxc.split(s)) for s in data.split(',,,ADMNSRC'))
 >>> print result
 one, two, three,four,,,,ADMNSRC, five,six
 one, two, three,four,,,,ADMNSRC,eighteen,   and so on

Bengt Richter

More information about the Python-list mailing list