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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:47:08+00:00 2026-06-08T02:47:08+00:00

I am writing the content of article to a text file. Source file: lol

  • 0

I am writing the content of “article” to a text file.

Source file:

lol hi
lol hello
lol text
lol test

Python:

for line in all_lines:
    if line.startswith('lol '):
        mystring = line.replace('lol ', '').lower().rstrip()

article = 'this is my saved file\n' + mystring + '\nthe end'

This is what gets saved to the txt file:

this is my saved file
test
the end

This is what I want saved to the txt file:

this is the saved file
hi
hello
test
text
the end
  • 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-08T02:47:10+00:00Added an answer on June 8, 2026 at 2:47 am

    You are replacing the string each time. You will want to store the results of each lol line and then add them all to mystring:

    mystring = []
    for line in all_lines:
        if line.startswith('lol '):
            mystring.append(line.replace('lol ', '', 1).lower().rstrip() + '\n')
    
    article = 'this is my saved file\n'+''.join(mystring)+'\nthe end'
    

    In the above code, I’ve turned mystring into list which is then turned into a string at the end using the join method. Note that I’ve added a newline (\n) character to each line as you want that character in your output (and rstrip() removes it). Alternatively, you can write:

    line.replace('lol ', '', 1).lower().rstrip(' ')
    

    which lets rstrip() only strip spaces and not all other forms of whitespace.


    Edit: An alternative approach is to write:

    mystring.append(line.replace('lol ', '').lower().rstrip())
    

    and:

    article = 'this is my saved file\n'+'\n'.join(mystring)+'\nthe end'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm have a StringBuilder that is writing content to a file. Towards the end
A software is producing UTF-8 files, but writing content to the file that isn't
I am writing a script for renaming/copying the content of a file. I have
I'm writing a script that extracts the content out of an article and removes
I'm writing a simple content management system. I need to store SHA1 hash values
What is the best way of writing the some content to the beginning and
I'm writing code to display html content in Qwebview, i did it by setHtml
I am writing a chrome extension that is a 'content script' I want to
I'm writing a form in XAML that has multiple buttons with content of different
I am having trouble writing query so that I can query the content of

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.