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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:04:15+00:00 2026-05-22T00:04:15+00:00

I have a piece of code which generates quite large lists in each iteration.

  • 0

I have a piece of code which generates quite large lists in each iteration. To save memory I want to write each list to a binary file in each iteration after the list has been generated.
I have tried this with text files(even setting the parameter to “wb” in linux). “wb” seems not to have any effect for the file to be written in binary or text format. Moreover, the written file is huge and I don’t want this. I am sure that If i can write these lists in binary format this file will be much smaller.
thanks

  • 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-22T00:04:15+00:00Added an answer on May 22, 2026 at 12:04 am

    Since you mentioned the need for compressibility, I’d suggest using pickle with the gzip module to compress your output. You can write and read back your lists one at a time, here’s an example of how:

    import gzip, pickle
    
    output = gzip.open('pickled.gz', 'wb', compresslevel=9)
    
    for x in range(10):
         output.write(pickle.dumps(range(10)) + '\n\n')
    output.close()
    

    And then use a generator to yield the lists back one at a time:

    def unpickler(input):
        partial = []
        for line in input:
            partial.append(line)
            if line == '\n':
                obj = ''.join(partial)
                partial = []
                yield pickle.loads(obj)
    
    input = gzip.open('pickled.gz', 'rb')
    for l in unpickler(input):
        print l
    
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we have a piece of code which generates a zip file on our system.
I want a piece of code which does not involve loops but automatically generates
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have a piece of code in ANSI C which uses the time.h library
I have a piece of fortran code, and I am not sure which standard
I have a little piece of rails code which lets users enter a review.
I am somewhat new to R, and i have this piece of code which
We have a piece of code which saves a .Net System.Drawing.Bitmap to file. The
I have this piece of code which works in Firefox link = $('<a>link</a>'); var
I have a piece of code which inserts a link into a p tag

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.