[Numpy-discussion] String manipulation
Christopher Barker
Chris.Barker at noaa.gov
Wed Jul 22 14:33:56 EDT 2009
Stéfan van der Walt wrote:
> You cannot use view on an array scalar, so you'll have to convert to
> an array first:
>
> x = x.reshape((1,))
> x.view('S1')
Bingo!
Thanks, Stéfan.
I don't know why one can't uwe view on a scalar -- there is still a dat
pointer isn't there?, and it can be reshaped. Butno matter, now I have a
one liner that splits a string up into fixed fields, and returns a
float array of the numbers in those fields:
np.array((line,)).view(dtype='S%i'%field_len).astype(np.float)
I suspect this is faster than the list comp method, though I haven't
tested it -- I"ll leave that to someone that actually has this problem
to solve ;-)
I've enclosed a bit of test code with a few other approaches as well.
-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 at noaa.gov
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: application/x-python
Size: 1041 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090722/e347ef57/attachment.bin>
More information about the NumPy-Discussion
mailing list