[Numpy-discussion] Converting string to array

eric jones eric at enthought.com
Mon May 12 23:11:07 CDT 2003


Try fromstring.  I think it should do what you want:

>>> from Numeric import *
>>> print fromstring.__doc__
fromstring(string, typecode='l', count=-1) returns a new 1d array
initialized from the raw binary data in string.  If count is positive, 
the new array will have count elements, otherwise it's size is
determined 
by the size of string.

eric

----------------------------------------------
eric jones                    515 Congress Ave
www.enthought.com             Suite 1614
512 536-1057                  Austin, Tx 78701 


> -----Original Message-----
> From: numpy-discussion-admin at lists.sourceforge.net [mailto:numpy-
> discussion-admin at lists.sourceforge.net] On Behalf Of Andrew Nesbit
> Sent: Monday, May 12, 2003 11:57 PM
> To: numpy-discussion at lists.sourceforge.net
> Subject: [Numpy-discussion] Converting string to array
> 
> I have a string representing a sequence of 16-bit audio samples, read
> in from an AIFF file like this:
> 
> f = aifc.open("filename.aiff", "r")
> data = f.readframes(f.getnframes())
> 
> I can convert this to a tuple of signed 16-bit integers using the
> Python standard library "array" module:
> 
> a = array.array("h", data)
> 
> This gives samples in the range [-32768, 32767] and everything is
> fine.
> 
> However, instead of using module array I want to use Numeric's array
> function in an equivalent way.  I've been trying to do it like this:
> 
> a = Numeric.array(data, Numeric.Int16)
> 
> Now, a.itemsize() outputs 2, which seems fine, but when I iterate over
> the resulting array (using standard a[i] notation), the results
> indicate that the array is being treated as a sequence of 8-bit,
> rather than 16-bit, integers.
> 
> What am I doing wrong here?
> 
> Thanks in advance.
> 
> Andrew.
> 
> 
> 
> 
> -------------------------------------------------------
> Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
> The only event dedicated to issues related to Linux enterprise
solutions
> www.enterpriselinuxforum.com
> 
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion






More information about the Numpy-discussion mailing list