在python 3.5中,typecode'c'不能用于数组定义

时间:2022-03-03 09:46:59

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

我正在使用python 3.5。我试图用一组字符定义一个数组。我试过以下。

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)

ValueError:错误的类型代码(必须是b,B,u,h,H,i,I,l,L,q,Q,f或d)

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

'c'不是字符的有效类型代码吗?请赐教。

3 个解决方案

#1


1  

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:

根据文档,python 3.5中不再提供'c'。它在python 2.6中可用,如此处所述。您可以使用有符号或无符号类型:

  • 'b': signed char
  • 'b':签名的char

  • 'B': unsigned char
  • 'B':unsigned char

Using 'b':

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

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

#2


1  

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

您可以在Python Shell中执行帮助(array.array)。

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 

#3


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.

它在Python 3中无效,但您可以使用'b'(带符号字符)或'B'(无符号字符)。你必须传递数字而不是字符串。您可以使用ord()将字符转换为数字值。 unicode字符有一个'u'代码,但不推荐使用。

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

Python 3中的所有字符串都是unicode。这意味着与C不同,单个字符不适合一个字节。如果你想要一个单字节数组,你可以使用类型代码'b'或'B',但你不能传入字符来初始化数组(因为字符不适合字节)。

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

这样就可以很容易地将字符转换成整数:

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

#1


1  

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:

根据文档,python 3.5中不再提供'c'。它在python 2.6中可用,如此处所述。您可以使用有符号或无符号类型:

  • 'b': signed char
  • 'b':签名的char

  • 'B': unsigned char
  • 'B':unsigned char

Using 'b':

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

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

#2


1  

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

您可以在Python Shell中执行帮助(array.array)。

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 

#3


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.

它在Python 3中无效,但您可以使用'b'(带符号字符)或'B'(无符号字符)。你必须传递数字而不是字符串。您可以使用ord()将字符转换为数字值。 unicode字符有一个'u'代码,但不推荐使用。

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

Python 3中的所有字符串都是unicode。这意味着与C不同,单个字符不适合一个字节。如果你想要一个单字节数组,你可以使用类型代码'b'或'B',但你不能传入字符来初始化数组(因为字符不适合字节)。

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

这样就可以很容易地将字符转换成整数:

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