Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to persist a data structure to a file in python?

Let's say I have something like this:

d = { "abc" : [1, 2, 3], "qwerty" : [4,5,6] } 

What's the easiest way to progammatically get that into a file that I can load from python later?

Can I somehow save it as python source (from within a python script, not manually!), then import it later?

Or should I use JSON or something?

like image 354
Blorgbeard Avatar asked Jun 26 '09 04:06

Blorgbeard


People also ask

How do you store data in a Python file?

We can use the write() method to put the contents of a string into a file or use writelines() if we have a sequence of text to put into the file. For CSV and JSON data, we can use special functions that Python provides to write data to a file once the file is open.

How does Python store data in a text file?

To write to a text file in Python, you follow these steps: First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.

Which data structure is fastest in Python?

Space-time tradeoff. The fastest way to repeatedly lookup data with millions of entries in Python is using dictionaries. Because dictionaries are the built-in mapping type in Python thereby they are highly optimized.


2 Answers

Use the pickle module.

import pickle d = { "abc" : [1, 2, 3], "qwerty" : [4,5,6] } afile = open(r'C:\d.pkl', 'wb') pickle.dump(d, afile) afile.close()  #reload object from file file2 = open(r'C:\d.pkl', 'rb') new_d = pickle.load(file2) file2.close()  #print dictionary object loaded from file print new_d 
like image 153
eric.christensen Avatar answered Oct 07 '22 17:10

eric.christensen


Take your pick: Python Standard Library - Data Persistance. Which one is most appropriate can vary by what your specific needs are.

pickle is probably the simplest and most capable as far as "write an arbitrary object to a file and recover it" goes—it can automatically handle custom classes and circular references.

For the best pickling performance (speed and space), use cPickle at HIGHEST_PROTOCOL.

like image 30
Miles Avatar answered Oct 07 '22 18:10

Miles