replacing multiple instances of commas beginning at specific position

Micah Elliott mde at micah.elliott.name
Mon Nov 14 14:22:28 EST 2005


On Nov 14, striker wrote:
> I have a comma delimited text file that has multiple instances of
> multiple commas.  Each file will contain approximatley 300 lines.
> For example:
> 
> one, two, three,,,,four,five,,,,six
> one, two, three,four,,,,,,,,,,eighteen,   and so on.
> 
> There is one time when multiple commas are allowed.  Just prior to
> the letters ADMNSRC there should be one instance of 4 commas. (
> ,eight,,,,ADMNSRC,thirteen, ).  The text ADMNSRC is NOT in the same
> place on each line.
> 
> 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?

One possible approach:

#! /usr/bin/env python

import re

# This list simulates the actual opened file.
infile = [
    'one, two, three,four,,,,,,ADMNSRC,,,,eighteen,',
    'one, two, three,four,five,six'
]

# Placeholder for resultant list.
result = []

for item in infile:
    # Use a regex to just reduce *all* multi-commas to singles.
    item = re.sub(r',{2,}', r',', item)
    # Add back the desired commas for special case.
    item = item.replace('ADMNSRC', ',,,ADMNSRC')
    # Remove spaces??
    item = item.replace(' ', '')
    # Add to resultant list.
    result.append(item)

-- 
_ _     ___
|V|icah |- lliott             <><             mde at micah.elliott.name
" "     """



More information about the Python-list mailing list