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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:23:14+00:00 2026-06-10T17:23:14+00:00

I’m adding some new bits to one of the lines in a text file

  • 0

I’m adding some new bits to one of the lines in a text file and then writing it along with the rest of the lines in the file to a new file. Referring to the 2nd if statement in the while loop, I want that to be all on the same line:

path = raw_input("Enter the name of the destination folder: ")

source_file = open("parameters")
lnum=1
for line in source_file:
    nums = line.split()
    if (lnum==10):
        mTot = float(nums[0])
    if (lnum==11):
        qinit = float(nums[0])
    if (lnum==12):
        qfinal = float(nums[0])
    if (lnum==13):
        qgrowth = float(nums[0])
    if (lnum==14):
        K = float(nums[0])
    lnum = lnum+1

q = qinit
m1 = mTot/(1+qinit)
m2 = (mTot*qinit)/(1+qinit)
taua = (1/3.7)*(mTot**(-4.0/3.0))
taue = taua/K
i = 1
infname = 'parameters'
while (q <= qfinal):
    outfname = path+'/'+str(i)
    oldfile = open(infname)
    lnum=1
    for line in oldfile:
        if (lnum==17):
            line = "{0:.2e}".format(m1)+' '+line
        if (lnum==18):
            line = "{0:.2e}".format(m2)+' '+line+' '+"{0:.2e}".format(taua)+' '+"      {0:.2e}".format(taue)
        newfile = open(outfname,'a')
        newfile.write(line)
        lnum=lnum+1
    oldfile.close()
    newfile.close()
    i=i+1
    q = q + q*(qgrowth)
    m1 = mTot/(1+q)
    m2 = (mTot*q)/(1+q)

but taua and taue are being written on the line below the rest of it. What am I missing here?

  • 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-10T17:23:15+00:00Added an answer on June 10, 2026 at 5:23 pm

    That is because line still contains the trailing newline, and when you concatenate it you are also including the newline.

    Insert a

    line = line.strip()
    

    right after the if (lnum == 19): but before you put the longer line together to get rid of the newline.

    Note that write will not add a newline automatically, so you’ll want to add a trailing newline of your own.

    UPDATE:

    This is untested, but I think unless I messed up, you could just use this instead of your longer line:

     line = line.strip()
     line = "{0:.2e} {} {0:.2e}   {0:.2e}\n".format(x, line, y, z)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.