Serialization, save type information in file and restore them

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 22 08:11:17 EDT 2010


En Fri, 18 Jun 2010 08:24:01 -0300, Timothy Wu <2huggie at gmail.com>  
escribió:

> I created a class that's able to manipulate tabulated data. I want to be
> able to dump the bulk of the data and other attributes as a tab-delimited
> text. I have trouble saving/restoring type information in the file. For
> example, some attributes are int, others may be float, etc. So I want to
> store the data type as well as the data value themselves in a file. And I
> don't think I want to use Pickle because I want it to be readily opened  
> in
> vi and be readable as a tab-delimited file and be able to import into  
> Excel
> as well.
>
> What's the best way to achieve this? I was able to write string like
> "attribute = int(value)" into a file. But how do I get the value back? I
> want the "int(value)" string to be loaded into the program and be  
> executable
> so I can actually create the instance variable in the class.

Use the csv module to write your data in the RDB file format.
The RDB format is fairly simple: first line contains the column names (tab  
separated), second line stores type information (tab separated), remaining  
lines contain actual data (also, tab separated).
(You may use any format you want, but why reinvent it, and you may even  
find an existing RDB parser/writer in Python)

-- 
Gabriel Genellina




More information about the Python-list mailing list