2

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.

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

'b' is an integer. You should convert your char to an integer using ord('char').
Thanks. I tried with 'b'. I am getting the following error: my_char_array = array.array('b', ['g','e','e','k']) TypeError: an integer is required (got type str)
Thank you. Do you have any idea why python made this change. Isn't it just easier to use 'c'? What is the need to read as 'b' with string conversion.
I do not know. Here: docs.python.org/3.0/whatsnew/… there is a possible explanation.
3

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 

1 Comment

A very well documented answer.
1

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

3 Comments

The 'u' type works. I don't understand what 'b' means and the need to pass as number and convert into characters :(.. I need to do some research. I am new to OOP.
@Harish I have added some explanation to my answer.
Thank you. This helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.