I am reading an XML file and reorganizing the desired data into Python data structures (lists, tuples, etc.)
For example, one of my XML parser modules produces the following data:
# data_miner.py
animals = ['Chicken', 'Sheep', 'Cattle', 'Horse']
population = [150, 200, 50, 30]
Then I have a plotter module that roughly says, e.g.:
# plotter.py
from data_miner import animals, population
plot(animals, population)
Using this method, I have to parse the XML file every time I do a plot. I'm still testing other aspects of my program and the XML file doesn't change as frequently for now. Avoiding the parse stage would dramatically improve my testing time.
This is my desired result:
In between data_miner.py
and plotter.py
, I want a file that contains animals
and population
such that they can be accessed by plotter.py
natively (e.g. no change in plotting code), without having to run data_miner.py
every time. If possible, it shouldn't be in csv
or any ASCII format, just a natively-accessible format. plotter.py
should now look roughly like:
# 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)
Analogy:
This is roughly equivalent to MATLAB's save
command that saves the active workspace's variables into a .mat
file. I'm looking for something similar to the .mat
file for Python.
Recent experience:
I have seen pickle
and cpickle
, but I'm not sure how to get it to work. If that is the right tool to use, example code would be very helpful. There may also be other tools that I don't know yet.