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 8047631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:09:56+00:00 2026-06-05T06:09:56+00:00

I am having trouble replacing three commas with one comma in a text file

  • 0

I am having trouble replacing three commas with one comma in a text file of data.

I am processing a large text file to put it into comma delimited format so I can query it using a database.

I do the following at the command prompt and it works:

  >>> import re
  >>> line = 'one,,,two'
  >>> line=re.sub(',+',',',line)
  >>> print line
  one,two
  >>>

following below is my actual code:

  with open("dmis8.txt", "r") as ifp:
      with open("dmis7.txt", "w") as ofp:
          for line in ifp:
              #join lines by removing a line ending.
              line=re.sub('(?m)(MM/ANGDEC)[\r\n]+$','',line)
              #various replacements of text with nothing. This removes the text
              line=re.sub('IDENTIFIER','',line)
              line=re.sub('PART','50-1437',line)
              line=re.sub('Eval','',line)
              line=re.sub('Feat','',line)
              line=re.sub('=','',line)
              #line=re.sub('r"++++"','',line)
              line=re.sub('r"----|"',' ',line)
              line=re.sub('Nom','',line)
              line=re.sub('Act',' ',line)
              line=re.sub('Dev','',line)
              line=re.sub('LwTol','',line)
              line=re.sub('UpTol','',line)
              line=re.sub(':','',line)
              line=re.sub('(?m)(Trend)[\r\n]*$',' ',line)
              #Remove spaces replace with semicolon
              line=re.sub('[ \v\t\f]+', ',', line)
              #no worky line=re.sub(r",,,",',',line)
              line=re.sub(',+',',',line)
              #line=line.replace(",+", ",")
              #line=line.replace(",,,", ",")
              ofp.write(line)

This is what i get from the code above:
There are several commas together. I don’t understand why they won’t get replaced down to one comma.
Never mind that I don’t see how the extra commas got there in the first place.

  50-1437,d
  2012/05/01
  00/08/27
  232_PD_1_DIA,PED_HL1_CR,,,12.482,12.478,-0.004,-0.021,0.020,----|++++
  232_PD_2_DIA_TOP,PED_HL2_TOP,,12.482,12.483,0.001,-0.021,0.020,----|++++
  232_PD_2_DIA,PED_HL2_CR,,12.482,12.477,-0.005,-0.021,0.020,----|++++
  232_PD_2_DIA_BOT,PED_HL2_BOT,,12.482,12.470,-0.012,-0.021,0.020,--|--++++

raw data for reference:

  PART IDENTIFIER         :  d
  2012/05/01
  00/08/27
  232_PD_1_DIA  Eval Feat =  PED_HL1_CR   MM/ANGDEC
                    Nom           Act           Dev         LwTol         UpTol         Trend
                 12.482        12.478        -0.004        -0.021         0.020     ----|++++

  232_PD_2_DIA_TOP  Eval Feat =  PED_HL2_TOP   MM/ANGDEC
                 12.482        12.483         0.001        -0.021         0.020     ----|++++

  232_PD_2_DIA  Eval Feat =  PED_HL2_CR   MM/ANGDEC
                 12.482        12.477        -0.005        -0.021         0.020     ----|++++

Can someone kindly point what I am doing wrong?

thanks in advance…

  • 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-06-05T06:09:57+00:00Added an answer on June 5, 2026 at 6:09 am

    Your regex is working fine. The problem is that it you concatenate the lines (by write()ing them) after you scrub them with your regex.

    Instead, use "".join() on all of your lines, run re.sub() on the whole thing, and then write() it all to the file at once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble with replacing a portion of a file in binary mode.
I'm having trouble replacing my link from text to an image in my Rails
I'm having trouble replacing a function from a different module with another function and
I'm trying to use sed to fix a file, and I'm having trouble grokking
I'm having trouble replacing things like joe. in a regular expression. Here's the code
I'm having trouble with zooming TIFF images loaded into a QGraphicsView with QGraphicsPixmapItem .
I'm having some trouble trying to animate some text around a circular path in
in Javascript, I am having some trouble replacing '/' with '/' in a string.
I'm having some trouble replacing the + sign with its urlencoded string of %2B.
i am having trouble replacing the modified date in my script via sed. I

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.