[Tutor] passing dictionaries through a file

David Heiser davidheiserca at gmail.com
Tue Mar 17 15:56:40 CET 2015



On 3/16/2015 5:04 PM, Alan Gauld wrote:
> 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.
>
>

The "shelve" module is ideally suited to storing dictionary type data to 
a file. It's a simplification of "pickle".




More information about the Tutor mailing list