[Numpy-discussion] Nested recarrays with subarrays and loadtxt: a bug in loadtxt?

Pierre GM pgmdevlist@gmail....
Wed May 27 18:03:11 CDT 2009


On May 27, 2009, at 6:15 PM, Fernando Perez wrote:

> Hi Pierre,
>
> On Wed, May 27, 2009 at 3:01 PM, Pierre GM <pgmdevlist@gmail.com>  
> wrote:
>> Have you tried np.lib.io.genfromtxt ?
>>
>
> I didn't know about it, but it has the same problem as loadtxt:

Oh yes indeed. Yet another case of "I-opened-my-mouth-too-soon'...

OK, so there's a trick. Kinda:
* Define a specific converter:

def block_converter(values):
     # Convert the strings to int
     val = [int(_) for _ in values.split()]
     new = np.array(val, dtype=int).reshape(2,3)
     out = tuple([tuple(_) for _ in new])
     return out

* Now, make sure that the column-delimiter is set to '\t' and use the  
new converter
data = genfromtxt(txtdata,dt, delimiter="\t",  
converters={3:block_converter})

That works if your second line is "aaaa	2.0	7.4	2 11 22 3 4 56"  
instead of "aaaa	2.0	7.4	2 11 22 3 4 5 6" (that is, if you have  
exactly 6 ints in the last entry, not 7). Note that youcould modify  
the converter to deal with that if needed.




More information about the Numpy-discussion mailing list