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?
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.
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.
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.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With