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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:38:24+00:00 2026-06-16T00:38:24+00:00

I have strings like this in a file: 2381 OH 209 SER OG 1049

  • 0

I have strings like this in a file:

  2381         OH    209    SER     OG   1049    -0.6546         16   ; qtot 1.543

and I want to replace some numbers (the 1st and the 6th, “2381” & “1049”) with different ones, but keeping the alignment, i.e. adding or removing blank spaces before the numbers as needed. That is, I would replace 2381 with __24 (_ is a blank) or _1049 with 37628.

I could hard-code the exact positions of each number, but those could be different with different files, and I’d like something more versatile.

Can anyone help me doing this in python? Say the code is something like:

# list_a and list_b contain two different mappings between integer numbers
for line in file:
  (a, b) = (int(line.split()[0]), int(line.split()[5]))
  c = list_a[a]
  d = list_b[b]
  # create "modline", as "line" where (a,b) are replaced with (c,d)
  print modline

In case it matters, the mappings list_a and list_b are just the order of appearance of the numbers a, b. So, if the input file has:

  2381         OH    209    SER     OG   1049    -0.6546         16   ; qtot 1.543
  2382         HO    209    SER     HG   1049     0.4275      1.008   ; qtot 1.971
  2379          C    209    SER      C   1048     0.5973      12.01   ; qtot 2.568
  2380          O    209    SER      O   1048    -0.5679         16   ; qtot 2

I want it to become:

     1         OH    209    SER     OG      1    -0.6546         16   ; qtot 1.543
     2         HO    209    SER     HG      1     0.4275      1.008   ; qtot 1.971
     3          C    209    SER      C      2     0.5973      12.01   ; qtot 2.568
     4          O    209    SER      O      2    -0.5679         16   ; qtot 2

because 2381 appears 1st, 2380 appears 4th; 1049 appears 1st (in its column), etc. So list_a[2381] = 1 and list_b[1049] = 1.

But I think I know how to do that, my problem now is actually replacing the numbers in the strings, taking into account the variable number of spaces.

I should add that there’s no guarantee that the numbers are unique in each line, so I can’t simply rely on regex match. I have to replace the 1st and 6th numbers, not “every (or the first) instance of 2381“

  • 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-16T00:38:25+00:00Added an answer on June 16, 2026 at 12:38 am

    Answering my own question, I think this does it:

    # list_a and list_b contain two different mappings between integer numbers
    for line in file:
      words = re.findall("(\s*\S+)", line)
      a = int(words[0])
      b = int(words[5])
      c = list_a[a]
      d = list_b[b]
      modline = str(c).rjust(len(words[0])) + "".join(words[1:5]) + str(d).rjust(len(words[5])) + "".join(words[6:])
      print modline
    

    That is, forget about the simple split and go regex from the beginning. What I can almost guarantee is that there will be enough spaces in the original string for the replacement string to fit there (otherwise I’d have to change the alignment in the original file, which is a different beast).

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

Sidebar

Related Questions

i have file like this 1248812832.v.doc and i want to remove strings and dots
Currently I have some code to replace strings in a file that looks like
i have 4 strings in my application like this that i want to pass
I have a data file containing a combination of doubles and strings like this:
I have a file with a bunch of strings that looks like this: new
I have an array of file-path strings like this videos/funny/jelloman.wmv videos/funny/bellydance.flv videos/abc.mp4 videos/june.mp4 videos/cleaver.mp4
Hi I have a html called 1.html file like this <div class=t_l></div> <some> lines
I have a .txt file,primary list, with strings like this: f r y h
I have lots of strings in a text file, like this: /home/mossen/Desktop/jeff's project/Results/FCCY.png /tmp/accept/FLWS14UU.png
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files

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.