[Numpy-discussion] loadtxt() behavior on single-line files

Christopher Barker Chris.Barker@noaa....
Thu Jun 24 13:48:24 CDT 2010


Warren Weckesser wrote:
> Benjamin Root wrote:
>> Note that this isn't restricted to structured arrays.  For regular 
>> ndarrays, loadtxt() appears to mimic the behavior of np.squeeze():
> 
> Exactly.  The last four lines of the function are:
> 
>     X = np.squeeze(X)
>     if unpack:
>         return X.T
>     else:
>         return X

> It would be useful to be able to tell loadtxt to not call squeeze, so a 
> program that reads column-formatted data doesn't have to treat the case 
> of a single line specially.

I agree -- it seem to me that every time I load data, I know what shape 
I expect the result to be -- I'd never want it to squeeze. It might be 
nice if you could specify the dimensionality of the array you want.


But for now: can you just do a reshape?

In [42]: strData = StringIO("53.2 49.2")

In[43]:a=p.loadtxt(strData,dtype=[('x',float),('y',float)]).reshape((-1,))

In [45]: a.shape
Out[45]: (1,)



-Chris




-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the NumPy-Discussion mailing list