[Tutor] passing dictionaries through a file

Alan Gauld alan.gauld at btinternet.com
Tue Mar 17 01:04:21 CET 2015


On 16/03/15 20:39, Doug Basberg wrote:
> I would like to pass the contents of a dictionary from one program to
> another through a file.  So, what is the elegant way to pass a dictionary by
> file?

The elegant way may be not to use a file.
A Python dictionary usually converts easily
to JSON format which can be sent directly
between processes over http. If you control
the receiving program.

Alternatively, you could save the JSON into a
file and pass that between processes. If the
receiving program knows about JSON.

Or you could just use the Python pickle module
to save the dictionary. If the receiving program
is in Python.

Or you could write the dictionary out to a CSV
file using the csv module. If the other program
is not under your control and needs to use
standard format files. (eg Excel)


> more about Python.  In C++, I was very happy using structs and classes with
> serialization.  Is the Python solution somewhat similar to C++?

You can do it manually but in Python there is usually
a better way. One of the options above should work
for you, it depends on how much control you have
over the receiving program code.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list