[Numpy-discussion] String manipulation

Christopher Barker Chris.Barker@noaa....
Tue Jul 21 10:56:08 CDT 2009


Pierre GM wrote:
> On Jul 20, 2009, at 3:44 PM, Christopher Barker wrote:
>> ...
>> Is there a cleaner way to do this?

> Yes. np.lib._iotools.LineSplitter and/or np.genfromtxt

Great, thanks -- though the underscore in _iotools implies that this 
isn't supposed to be general purpose tools.

Also, aside from the problem at hand, what I was getting at was whether 
there is a cleaner way to go from a string to a numpy array of 
characters or strings.

I see that LineSplitter() uses a list comprehension to do the slicing, 
and I was looking for a way (perhaps needlessly) to use numpy instead, 
which requires an efficient way to get a numpy array from a string.

I don't see why:

np.array('a string', dtype='S1')

results in a length (1,) array, for instance.

Actually, I think I do -- numpy is treating the string as a single 
scalar, rather than a sequence of characters, and doing its best to 
convert that scaler to a length one string. However, I don't know if 
there is a compelling reason why it should do that -- in other contexts, 
Python generally treats strings as a sequence of characters.

-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