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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:39:17+00:00 2026-06-05T14:39:17+00:00

I want to know how to save an array to a file. You already

  • 0

I want to know how to save an array to a file. You already helped me a lot, but I have more naive questions (I’m new to Python):

@<TRIPOS>MOLECULE 
NAME123 
line3 
line4 
line5 
line6 
@<TRIPOS>MOLECULE 
NAME434543 
line3 
line4 
line5 
@<TRIPOS>MOLECULE 
NAME343566 
line3 
line4 

I am currently have this code, but it’s save only the last item from the array no the all listed in the items_grep. How to fix this?

items = []
with open("test.txt", mode="r") as itemfile: 
    for line in itemfile: 
        if line.startswith("@<TRIPOS>MOLECULE"): 
            items.append([]) 
            items[-1].append(line) 
        else: 
            items[-1].append(line)      
#
# list to grep
items_grep = open("list.txt", mode="r").readlines()
# writing files
for i in items:
    if i[1] in items_grep:
        open("grep.txt", mode="w").write("".join(i))

Thank you in advance!

  • 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-05T14:39:18+00:00Added an answer on June 5, 2026 at 2:39 pm

    The reason your file is only showing the last value is because every time you open the file with the w flag, it erases the existing file. If you open it once and then use the file object, you’ll be fine, so you’d do (note, this is not a very clean/pythonic way of doing it, just being clear about how the open command works)

    myfile = open("grep.txt", "w")
    for i in ...
        if i[1] ...:
             myfile.write(i + '\n')
    

    Easy way to handle this would be to do a list comprehension first and then join, e.g.:

    newstr = '\n'.join([''.join(i) for i in items if i[1] in items_grep])
    

    Then just write the entire string to the file at once. Note that without adding the \n between items, you won’t end up with each item on a new line, instead they will all be added one after the other without spaces.

    You should also consider using the with keyword to auto close the file.

    with open("grep.txt","w") as f:
        f.write(newstr)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save a remote img-file to my server, but I don't know
I want to know whether it is possible to save as an array list
I don't know if this is possible. I want to save a number after
I have an android app it displays web view.I want know the app idle
I have a FileUpload control. I am trying to save the file that is
I want to know how to put the content inside an array into a
I have a newtype I'd like to save in a file, something like this:
I am writing a word puzzle game. I want to have a plist file
I have some reference data in a text file (~5MB) that I want to
I have a hash of hashes (@post) and I want to save it into

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.