[Numpy-discussion] genfromtxt converter question
Derek Homeier
derek@astro.physik.uni-goettingen...
Fri Jun 17 16:24:08 CDT 2011
On 17.06.2011, at 11:01PM, Olivier Delalleau wrote:
>> You were just overdoing it by already creating an array with the converter, this apparently caused genfromtxt to create a structured array from the input (which could be converted back to an ndarray, but that can prove tricky as well) - similar, if you omit the dtype=None. The following
>>
>> cnv = dict.fromkeys(range(4), lambda x: complex(*eval(x)))
>> b = np.genfromtxt(a,converters=cnv, dtype=None, delimiter=18, usecols=range(4))
>>
>> directly produces a shape(4,4) complex array for me (you may have to apply an .astype(np.complex64) afterwards if so desired).
>>
>> BTW I think this is an interesting enough case of reading non-trivially structured data that it deserves to appear on some examples or cookbook page.
>>
>> HTH,
>> Derek
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
> I had tried that as well and it doesn't work with numpy 1.4.1 (I get an object array). It may have been fixed in a later version.
OK, I was using the current master from github, but it works in 1.6.0 as well. I still noticed some differences between loadtxt and genfromtxt behaviour, e.g. where loadtxt would be able to take a string from the converter and automatically convert it to a number, whereas in genfromtxt the converter still had to include the float() or complex()...
Derek
More information about the NumPy-Discussion
mailing list