[Tutor] Structured files?

Alexandre Conrad alexandre.conrad at gmail.com
Thu Jun 2 17:57:51 CEST 2011


2011/6/2 Válas Péter <sulinet at postafiok.hu>:
> I can create files and write strings/unicodes.
> Is it possible to write a list, a dictionary or an object or anything into a
> file? Or do I have to transform them to strings?

As suggested by Walter, you should use the Pickle module to serialize
your Python objects so they can be written to files and read back to
Python (de-serialized). This way you can also share your pickled
objects with other Python apps and Pickle is great for this. Actually,
that's how the multiprocessing module works under the hood to pass
back and forth Python objects between processes.

If you are only going to serialize data structures such as lists,
dictionaries with data types such as strings, numbers, bools, None and
you want to share that data between non-Python application, I could
also suggest the use of the JSON module. JSON is a standard format
(see json.org) supported by many programming languages. JSON is very
popular for web service APIs. Your Python server could expose web APIs
and applications written in different programming languages (Java, C,
C#, Javascript ...) can call an API and interpret the JSON data your
server sends back.

This is some food for thought.

HTH,
-- 
Alex | twitter.com/alexconrad


More information about the Tutor mailing list