Python RegEx nested search and replace
Question: I need to to a RegEx search and replace of all commas found inside of quote
blocks.  
i.e.

    
    
    "thing1,blah","thing2,blah","thing3,blah",thing4  
    

needs to become

    
    
    "thing1\,blah","thing2\,blah","thing3\,blah",thing4  
    

my code:

    
    
    inFile  = open(inFileName,'r')
    inFileRl = inFile.readlines()
    inFile.close()
    
    p = re.compile(r'["]([^"]*)["]')
    for line in inFileRl:
        pg = p.search(line)
        # found comment block
        if pg:
            q  = re.compile(r'[^\\],')
            # found comma within comment block
            qg = q.search(pg.group(0))
            if qg:
                # Here I want to reconstitute the line and print it with the replaced text
                #print re.sub(r'([^\\])\,',r'\1\,',pg.group(0))
    

I need to filter only the columns I want based on a RegEx, filter further,  
then do the RegEx replace, then reconstitute the line back.

How can I do this in Python?


Answer: The `csv` module is perfect for parsing data like this as `csv.reader` in the
default dialect ignores quoted commas. `csv.writer` reinserts the quotes due
to the presence of commas. I used `StringIO` to give a file like interface to
a string.

    
    
    import csv
    import StringIO
    
    s = '''"thing1,blah","thing2,blah","thing3,blah"
    "thing4,blah","thing5,blah","thing6,blah"'''
    source = StringIO.StringIO(s)
    dest = StringIO.StringIO()
    rdr = csv.reader(source)
    wtr = csv.writer(dest)
    for row in rdr:
        wtr.writerow([item.replace('\\,',',').replace(',','\\,') for item in row])
    print dest.getvalue()
    

result:

    
    
    "thing1\,blah","thing2\,blah","thing3\,blah"
    "thing4\,blah","thing5\,blah","thing6\,blah"
    

