A Wednesday 07 May 2008, B Clowers escrigué: > Pythonians, > > I'm interested in using pytables to store some data in which some of > the information is stored in the form of a python dictionary. The > problem is as follows. How exactly do I save both the keys and > values from a dictionary that contains values of different python > types. For example if I have the following dictionary: > > import numpy as N > test_dict = dict(a = 3.14159, b = 'some string', c = [1,2,3,4,5], d = > N.arange(0,10,0.1)) > > How can I commit this structure to disk using PyTables? Do I need to > break it up into its individual components and then store them as > individual pieces using test_dict.iteritems (this seems unattractive) > or is there a better way? Any help would be appreciated.
In general yes, when using dictionaries you need to do break it up in individual components before being able to feed the table. If you want to avoid this, you can always use recarrays, that allows you to load them into tables in a clean and effective way. Look at this example: import tables as T import numpy as N ra = N.array([(3.14159,'some string',[1,2,3,4,5],N.arange(0,10,0.1))], dtype = "f8,S20,(5,)i4,(100,)f8") f = T.openFile("test.h5", "w") t = f.createTable(f.root, 'table', ra) print "table metadata-->", repr(t) print "table data-->", t[:] f.close() Hope this helps, -- Francesc Alted ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ Pytables-users mailing list Pytables-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pytables-users