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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:04:35+00:00 2026-05-21T19:04:35+00:00

When I print the group print(a) the entire group is shown. When I save

  • 0

When I print the group “print(a)” the entire group is shown. When I save it to a text file “open(“sirs1.txt”, “w”).write(a)” only the last row is saved to the file.

import re

def main():
f = open('sirs.txt')
for lines in f:
    match = re.search('(AA|BB|CC|DD)......', lines)
    if match:
        a = match.group()
        print(a)
        open("sirs1.txt", "w").write(a)

How do I save the entire group to the text file.

  • 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-05-21T19:04:36+00:00Added an answer on May 21, 2026 at 7:04 pm

    nosklo is correct the main problem is that you are overwriting the whole file each time you write to it. mehmattski is also correct in that you will also need to explicitly add \n to each write in order to make the output file readable.

    Try this:

    enter code here
    
    import re
    
    def main():
      f = open('sirs.txt') 
      outputfile = open('sirs1.txt','w')
    
      for lines in f:
        match = re.search('(AA|BB|CC|DD)......', lines)
        if match:
          a = match.group()
          print(a)
          outputfile.write(a+"\n")
    
      f.close()
      outputfile.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I print "\[\e[34m\]sometext" I get some text in blue, but can I specify
Unit testing and ASP.NET web applications are an ambiguous point in my group. More
Print all 12 verses of the popular holiday song . By 12 verses I
The print functionality of Excel (using VBA) is extremely slow. I'm hoping someone has
Consider: print $foo, AAAAAAAA, $foo, BBBBBBBB; Let's say I want to use this code
I want to print the first 10000 prime numbers. Can anyone give me the
When I try to print a string in a Windows console, sometimes I get
I need to print out data into a pre-printed A6 form (1/4 the size
Can i print out a url /admin/manage/products/add of a certain view in a template?
How can I elegantly print the date in RFC822 format in Perl?

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.