On 23 Jan 2012, at 22:07, Derek Homeier wrote:
In [4]: r = np.ones(3,dtype=[('name', '|S5'), ('foo', '<i8'), ('bar', '<f8')])
In [5]: r.tofile('toto.txt',sep='\n')
bash-4.2$ cat toto.txt ('1', 1, 1.0) ('1', 1, 1.0) ('1', 1, 1.0)
cnv = {0: lambda s: s.lstrip('('), -1: lambda s: s.rstrip(')')} r2 = np.loadtxt('toto.txt', delimiter=',', converters=cnv, dtype=r.dtype)
Generally loadtxt works more smoothly together with savetxt, but the latter unfortunately does not offer an easy way to save structured arrays (note to self and others currently working on npyio: definitely room for improvement!).
For the record, in that example np.savetxt('toto.txt', r, fmt='%s,%d,%f') would work as well, saving you the custom converter for loadtxt - it could just become tedious to work out the format for more complex structures, so an option to construct this automatically from r.dtype could certainly be a nice enhancement. Just wondering, is there something like the inverse operator to np.format_parser, i.e. mapping each dtype to a default print format specifier? Cheers, Derek