1

I want to write an array and a dictionary to a file (and possible more), and then be able to read the file later and recreate the array and dictionary from the file. Is there a reasonable way to do this in Python?

2
  • Sounds like you should use pickle Commented Dec 11, 2014 at 18:16
  • What kind of array? A numpy array or a list? Commented Dec 11, 2014 at 18:18

3 Answers 3

2

I recommend you use shelve (comes with python). For example:

import shelve
d = shelve.open('file.txt')           # in this file you will save your variables
d['mylist'] = [1, 2, 'a']             # thats all, but note the name for later.
d['mydict'] = {'a':1, 'b':2}
d.close()

To read values:

import shelve
d = shelve.open('file.txt')
my_list = d['mylist']           # the list is read from disk
my_dict = d['mydict']           # the dict is read from disk

If you are going to be saving numpy arrays then I recommend you use joblib which is optimized for this use case.

Sign up to request clarification or add additional context in comments.

Comments

0

Pickle would be one way to go about it (it is in the standard library).

import pickle

my_dict = {'a':1, 'b':2}

# write to file
pickle.dump(my_dict, open('./my_dict.pkl', 'wb'))

#load from file
my_dict = pickle.load(open('./my_dict.pkl', 'rb'))

And for the array you can use the ndarray.dump() method in numpy, which is more efficient for large arrays.

import numpy as np
my_ary = np.array([[1,2], [3,4]])
my_ary.dump( open('./my_ary.pkl', 'wb'))

But you can of course also put everything into the same pickle file or use shelve (which uses pickle) like suggested in the other answer.

4 Comments

Yes, what it basically does is serializing any python object to into a byte stream. You can save any Python object this way...
What if it is an array of tuples? Does it handle tuples?
Sure, tuples are Python objects ;) You can just see it yourself: pickle.dump([(1,2), (3,4)], open('test.pkl', 'wb')) , t = (pickle.load(open('test.pkl', 'rb'))), print(t) [(1, 2), (3, 4)]
No, you haven't. Why don't you just try it out on your data struct, then you will know for sure
0

The pickle format blurs the line between data and code and I don't like using it except when I'm the only writer and reader of the data in question and when I'm sure that it's not been tampered with.

If your data structure is just non sequence types, dicts and lists, you can serialise it into json using the json module. This is a pure data format which can be read back reliably. It doesn't handle tuples though but treats them as lists.

Here's an example.

 a = [1,2,3,4]
 b = dict(lang="python", author="Guido")
 import json
 with open("data.dump", "r") as f:
    x, y = json.load(f)
 print x  # => [1, 2, 3, 4]
 print y  # =>  {u'lang': u'python', u'author': u'Guido'}

It's not totally unchanged but often good enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.