How to save, then restore the original Python data structures to and from a file?

I am reading an XML file and reorganizing the necessary data into Python data structures (lists, tuples, etc.).

For example, one of my XML parser modules creates the following data:

# data_miner.py
animals = ['Chicken', 'Sheep', 'Cattle', 'Horse']
population = [150, 200, 50, 30]

Then I have a plotter module that roughly matches, for example:

# plotter.py
from data_miner import animals, population

plot(animals, population)

Using this method, I have to parse the XML file every time I make a plot. I am still testing other aspects of my program, and the XML file does not change as often as it is now. Avoiding the parsing step would greatly improve the testing time.

:
data_miner.py plotter.py , , animals population, plotter.py (, ), data_miner.py . , csv ASCII, . plotter.py :

# plotter.py

# This line may not necessarily be a one-liner.
from data_file import animals, population

# But I want this portion to stay the same
plot(animals, population)

:
MATLAB save, .mat. - .mat Python.

:
pickle cpickle, , . , . , .

+3
4

pickle cPickle .

:

# data_miner.py
import pickle

animals = ['Chicken', 'Sheep', 'Cattle', 'Horse']
population = [150, 200, 50, 30]

with open('data_miner.pik', 'wb') as f:
  pickle.dump([animals, population], f, -1)

# plotter.py
import pickle

with open('data_miner.pik', 'rb') as f:
    animals, population = pickle.load(f)

print animals, population

data_miner.py , ( , , ). (, ) , globals() .

, globals(), , ; , , , _ , ( import pickle as _pickle, with open ... as _f ..) globals() == , pickle.load dict, . list ( dict, ;-) , , , "" .

+5

, , Python, . - , JSON .

>>> json.dumps(['Chicken', 'Sheep', 'Cattle', 'Horse'])
'["Chicken", "Sheep", "Cattle", "Horse"]'
>>> json.dump(['Chicken', 'Sheep', 'Cattle', 'Horse'], sys.stdout) ; print
["Chicken", "Sheep", "Cattle", "Horse"]
>>> json.loads('["Chicken", "Sheep", "Cattle", "Horse"]')
[u'Chicken', u'Sheep', u'Cattle', u'Horse']
+2

picklewas designed for this. Use pickle.dumpto write an object to a file and pickle.loadto read it.

>>> data
{'animals': ['Chicken', 'Sheep', 'Cattle', 'Horse'], 'population': [150, 200, 50, 30]}
>>> f = open('spam.p', 'wb')
>>> pickle.dump(data, f)
>>> f.close()
>>> f = open('spam.p', 'rb')
>>> pickle.load(f)
{'animals': ['Chicken', 'Sheep', 'Cattle', 'Horse'], 'population': [150, 200, 50, 30]}
+1
source

As already mentioned, brine is commonly used here. Keep in mind that not everything is serializable (i.e. Files, sockets, database connections).

With simple data structures, you can also choose json or yaml. The latter is actually quite readable and editable.

0
source

Source: https://habr.com/ru/post/1762030/


All Articles