IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
11.3 Character array functions


11.3 Character array functions

array( buffer=None, itemsize=None, shape=None, byteoffset=0, bytestride=None, kind=CharArray)
The function array is, for most practical purposes, all a user needs to know to construct a character array.

The first argument, buffer, may be any one of the following:

(1) None (default). The constructor will allocate a writeable memory buffer which will be uninitialized. The user must assign valid data before trying to read the contents or before writing the character array to a disk file.

(2) a Python string containing binary data. For example:

     >>> print str.array('abcdefg'*10, itemsize=10)
     ['abcdefgabc', 'defgabcdef', 'gabcdefgab', 'cdefgabcde', 'fgabcdefga',
      'bcdefgabcd', 'efgabcdefg']

(3) a Python file object for an open file. The data will be copied from the file, starting at the current position of the read pointer.

(4) a character array. This results in a deep copy of the input character array; any other arguments to array() will be silently ignored.

     >>> print str.array(s)
     ['abcdefgabc', 'defgabcdef', 'gabcdefgab', 'cdefgabcde', 'fgabcdefga', 
      'bcdefgabcd', 'efgabcdefg']

(5) a nested sequence of strings. The sequence nesting implies the shape of the string array unless shape is specified.

     >>> print str.array([['Smith', 'Johnson'], ['Williams', 'Miller']])
     [['Smith', 'Johnson'],
      ['Williams', 'Miller']]

itemsize can be used to increase or decrease the fixed size of an array element relative to the natural itemsize implied by any literal data specified by the buffer parameter.

     >>> print str.array([['Smith', 'Johnson'], ['Williams', 'Miller']], 
                         itemsize=2)
     [['Sm', 'Jo'],
      ['Wi', 'Mi']])
     >>> print str.array([['Smith', 'Johnson'], ['Williams', 'Miller']], 
                         itemsize=20)
     [['Smith', 'Johnson'],
      ['Williams', 'Miller']]

shape is the shape of the character array. It can be an integer, in which case it is equivalent to the number of rows in a table. It can also be a tuple implying the character array is an N-D array with fixed length strings as its elements. shape should be consistent with the number of elements implied by the data buffer and itemsize.

byteoffset indicates an offset, specified in bytes, from the start of the array buffer to where the array data actually begins. byteoffset enables the character array to be offset from the beginning of a table record. This is mainly useful for implementing record arrays.

bytestride indicates the separation, specified in bytes, between successive elements in the last dimension of the character array. bytestride is used in the implementation of record arrays to space character array elements with the size of the total record rather than the size of a single string.

kind is used to specify the class of the created array, and should be RawCharArray, CharArray, or a subclass of either.

num2char( n, format, itemsize=32)
num2char formats the numarray n using the Python string format format and stores the result in a character array with the specified itemsize
     >>> num2char(num.arange(0.0,5), '%2.2f')
     CharArray(['0.00', '1.00', '2.00', '3.00', '4.00'])

Send comments to the NumArray community.