13

After running a lot of scripts in QGIS' python console, I have more than 10 dictionaries and lists, and I need these for further analysis. Is there a way to save python session, with all these variables?

In general, my question is how can I export my variables from python console to something, so that they can be loaded in again at a future time?

Thanks

1 Answer 1

15

If you want to access your results in other Python scripts, try the pickle module.

simple example:

>>> a = [1, 2, 3, 4, 5]
>>> import pickle
>>> f1 = open('results.pickle', 'wb')
>>> pickle.dump(a, f1)
>>> f1.close()

Now the data is saved ('pickled') in the results.pickle file.

To load the pickled data:

>>> f2 = open('results.pickle', 'rb')
>>> b = pickle.load(f2)
>>> b
[1, 2, 3, 4, 5]

pickle serializes objects for use within Python. If you want to use your data with other langauges/programs, try the json module, which has a very similar interface.

1
  • 1
    Is there a way to pickle pyqgis objects? (e.g. QgsVectorLayer)
    – cefect
    Commented Mar 6, 2021 at 19:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.