Posts: 23
Threads: 15
Joined: May 2019
I am not able to convert array to string using fromstring() method
my code is:
from array import *
my_char_array = array('c', ['g','e','e','k']) Error: Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
my_char_array = array('c', ['g','e','e','k'])
ValueError: bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jun-28-2019, 11:43 AM
(This post was last modified: Jun-28-2019, 11:43 AM by perfringo.)
I think that error message can't be clearer (can one observe 'c' in allowed typecodes?)
One can always do:
>>> import array
>>> help(array.array)
Help on class array in module array:
class array(builtins.object)
| array(typecode [, initializer]) -> array
|
| Return a new array whose items are restricted by typecode, and
| initialized from the optional initializer value, which must be a list,
| string or iterable over elements of the appropriate type.
|
| 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
|
| NOTE: The 'u' typecode corresponds to Python's unicode character. On
| narrow builds this is 2-bytes on wide builds this is 4-bytes.
|
| NOTE: The 'q' and 'Q' type codes are only available if the platform
| C compiler used to build Python supports 'long long', or, on Windows,
| '__int64'.
|
| Methods:
|
| append() -- append a new item to the end of the array
| buffer_info() -- return information giving the current memory info
| byteswap() -- byteswap all the items of the array
| count() -- return number of occurrences of an object
| extend() -- extend array by appending multiple elements from an iterable
| fromfile() -- read items from a file object
| fromlist() -- append items from the list
| frombytes() -- append items from the string
| index() -- return index of first occurrence of an object
| insert() -- insert a new item into the array at a provided position
| pop() -- remove and return item (default last)
| remove() -- remove first occurrence of an object
| reverse() -- reverse the order of the items in the array
| tofile() -- write all items to a file object
| tolist() -- return the array converted to an ordinary list
| tobytes() -- return the array converted to a string
|
| Attributes:
|
| typecode -- the typecode character used to create the array
| itemsize -- the length in bytes of one array item
|
/.../
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 2,125
Threads: 11
Joined: May 2017
Jun-28-2019, 12:29 PM
(This post was last modified: Jun-28-2019, 12:32 PM by DeaD_EyE.)
array('B', b'geek') B for unsigned byte. Using the fact that array takes an iterable and iterating over bytestrings return integers.
By the way, the star import is a bad habbit.
Use instead:
from array import array Imports from modle array the name array. In this case array is a function.
Posts: 23
Threads: 15
Joined: May 2019
I tried all options but no type code is understanding character data.......
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jul-03-2019, 11:54 AM
(This post was last modified: Jul-03-2019, 11:55 AM by perfringo.)
(Jun-28-2019, 11:01 AM)srm Wrote: I am not able to convert array to string
...
I tried all options
You haven't defined what you want.
Either this:
>>> import array
>>> array.array('u', ['g', 'e', 'e', 'k'])
array('u', 'geek') or something along this line:
>>> ''.join(array.array('u', ['g', 'e', 'e', 'k']))
'geek'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 7,319
Threads: 123
Joined: Sep 2016
Jul-03-2019, 01:11 PM
(This post was last modified: Jul-03-2019, 01:11 PM by snippsat.)
Is there any reason why you use array and not list (common way in most cases)?
Usage of array look at this answer.
>>> lst = ['g','e','e','k']
>>> lst
['g', 'e', 'e', 'k']
>>> type(lst)
<class 'list'>
>>>
>>> ''.join(lst)
'geek'
|