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

  • Home
  • SEARCH
  • 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 8767543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:50:33+00:00 2026-06-13T16:50:33+00:00

I have a text file containing decimal numbers throughout it and was wanting to

  • 0

I have a text file containing decimal numbers throughout it and was wanting to round up any that have a decimal >= 0.5, otherwise I want to truncate the decimal, e.g., 23.7897 becomes 24 and 17.2395 becomes 17.

The < 0.5 case is not a problem for re.sub:

re.sub(r'(\d+)\.[0-4]\d*', r'\1', line)

however, for the >= 0.5 case, I was wondering if there was a way to manipulate the \1 capture group during the substitution somehow? Is it stored anywhere so that it can be sent to a function, e.g.:

re.sub(r'(\d+)\.[5-9]\d*', roundUp('\1'), line)

where roundUp() returns a string representing the value \1 + 1.

Surely there is a way to just increment \1 inline during substitution without having loop through the matches from findall, incrementing the integer part, then refinding the pattern with re.sub and subtitute it in?

  • 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-13T16:50:34+00:00Added an answer on June 13, 2026 at 4:50 pm

    You can use an arbitrary callable for the replacement, so does this work for you?

    re.sub(r'\d+\.\d*', lambda match: str(int(round(float(match.group(0))))), line)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file containing the row numbers of the rows that should
In a text file I will have a line containing a series of numbers,
I have a big text file containing data that needs to be extracted and
I have a text file containing 10 columns of numbers. What I would like
I have text file containing a list of 16 bit hex numbers (e.g. '61C7393AA9B3474DB081C7B7CCE1C545')
Hi I have made a program that reads a text file containing words and
I have a text file containing the output of a recursive directory listing that
I have a text file containing a giant list of line numbers which I
I have a text file containing three columns of numbers; one column each for
I have a text file containing the first 1000 prime numbers and I have

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.