Harish
Harish

Reputation: 295

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

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.

Upvotes: 2

Views: 7810

Answers (3)

khelwood
khelwood

Reputation: 59111

It's not valid in Python 3, but you can use 'b' (signed character) or 'B' (unsigned character). You have to pass numbers instead of strings though. You can convert characters to numerical values using ord(). There is a 'u' code for unicode characters, but it is deprecated.

https://docs.python.org/3/library/array.html

Explanation:

All strings in Python 3 are unicode. That means that unlike in C, a single character does not fit into one byte. If you want an array of single bytes, you can use type code 'b' or 'B', but you can't pass characters in to initialise the array (since characters don't fit into bytes).

It's easy enough to convert characters to integers on the fly like this:

array.array('b', map(ord, 'Some characters'))

Upvotes: 1

neehari
neehari

Reputation: 2612

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 

Upvotes: 3

Miguel Isla
Miguel Isla

Reputation: 1460

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')])

Upvotes: 4

Related Questions