Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6649987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:50:23+00:00 2026-05-26T00:50:23+00:00

I need to to a RegEx search and replace of all commas found inside

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:50:24+00:00Added an answer on May 26, 2026 at 12:50 am

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With Regex, I need to find and replace all the mailaddresses in a fully
I need to use regex to search through an html file and replace href=pagename
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
i need a regex to replace a string. <span class=\Translation\ lang=\ThisLanguage\> with this one:
As programmer I need to filter, search and validate data every day. For all
I need to do a search and replace on long text strings. I want
I need to perform a search/replace on text which contains a comma which is
Possible Duplicate: What is the ultimate postal code and zip regex? I need Regex
I need a regex to count the number of columns in a pipe delimited
I need a regex for date format in python I want March 29 but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.