Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typecode 'c' is not available for array definition in python 3.5

Tags:

python

arrays

I am using python 3.5. I am trying to define an array with set of characters. I tried the below.

import array
my_char_array = array.array('c', ['g','e','e','k'])

When I run, I get the following error:

ValueError: bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)

Isn't 'c' a valid typecode for characters? Please enlighten me.

like image 622
Harish Avatar asked Feb 14 '26 03:02

Harish


2 Answers

According to the docs, 'c' is not anymore available in python 3.5. It was available in python 2.6 as documented here. You may use signed or unsigned types:

  • 'b': signed char
  • 'B': unsigned char

Using 'b':

m = array.array('b', [ord('g'), ord('e'), ord('e'), ord('k')])

like image 199
Miguel Isla Avatar answered Feb 16 '26 18:02

Miguel Isla


You could do help(array.array) in your Python Shell.

Arrays represent basic values and behave very much like lists, except the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character. The following type codes are defined:

 Type code   C Type             Minimum size in bytes 
  'b'         signed integer     1 
  'B'         unsigned integer   1 
  'u'         Unicode character  2 (see note) 
  'h'         signed integer     2 
  'H'         unsigned integer   2 
  'i'         signed integer     2 
  'I'         unsigned integer   2 
  'l'         signed integer     4 
  'L'         unsigned integer   4 
  'q'         signed integer     8 (see note) 
  'Q'         unsigned integer   8 (see note) 
  'f'         floating point     4 
  'd'         floating point     8 
like image 32
srikavineehari Avatar answered Feb 16 '26 18:02

srikavineehari