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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:28:48+00:00 2026-06-15T20:28:48+00:00

I am creating a very large array. Rather than having this array stored in

  • 0

I am creating a very large array. Rather than having this array stored in memory, I want to be able to write it to a file. This needs to be in a format I can later import.

I would use pickle but it appears pickle is used for completed file structures.

In the following example, I need a way for the out variable to be a file rather than a memory stored object:

out = []
for x in y:
    z = []
    #get lots of data into z
    out.append(z)
  • 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-15T20:28:49+00:00Added an answer on June 15, 2026 at 8:28 pm

    Take a look at streaming-pickle.

    streaming-pickle allows you to save/load a sequence of Python data structures to/from disk in a streaming (incremental) manner, thus using far less memory than regular pickle.

    It’s actually just a single file with three short methods. I added a snippet with an example:

    try:
        from cPickle import dumps, loads
    except ImportError:
        from pickle import dumps, loads
    
    
    def s_dump(iterable_to_pickle, file_obj):
        """ dump contents of an iterable iterable_to_pickle to file_obj, a file
        opened in write mode """
        for elt in iterable_to_pickle:
            s_dump_elt(elt, file_obj)
    
    def s_dump_elt(elt_to_pickle, file_obj):
        """ dumps one element to file_obj, a file opened in write mode """
        pickled_elt_str = dumps(elt_to_pickle)
        file_obj.write(pickled_elt_str)
        # record separator is a blank line
        # (since pickled_elt_str might contain its own newlines)
        file_obj.write('\n\n')
    
    def s_load(file_obj):
        """ load contents from file_obj, returning a generator that yields one
            element at a time """
        cur_elt = []
        for line in file_obj:
            cur_elt.append(line)
    
            if line == '\n':
                pickled_elt_str = ''.join(cur_elt)
                elt = loads(pickled_elt_str)
                cur_elt = []
                yield elt
    

    Here’s how you could use it:

    from __future__ import print_function
    import os
    import sys
    
    if __name__ == '__main__':
        if os.path.exists('obj.serialized'):
            # load a file 'obj.serialized' from disk and 
            # spool through iterable      
            with open('obj.serialized', 'r') as handle:
                _generator = s_load(handle)
                for element in _generator:
                    print(element)
        else:
            # or create it first, otherwise
            with open('obj.serialized', 'w') as handle:
                for i in xrange(100000):
                    s_dump_elt({'i' : i}, handle)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a very large raw binary data file, I will be creating a large
I'm creating app which uses very large amount of memory. When I finish computation
I'm creating a very large XML file (700mb +) that process large amounts of
I want to use NSFileHandle to write large text files to avoid handling very
I am creating a very large PHP MVC-based site that will have a large
I have a very large script which is creating multiple views. In a number
I'm creating a very simple django upload application but I want to make it
I am creating a very simple web service and I don't want to bother
I'm having a lot of trouble performing a very basic task: resizing an array.
I'm creating and processing a very large data set, with about 34 million data

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.