Python Forum
converting array to and from string in python 3.7.2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
converting array to and from string in python 3.7.2
#1
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)
Reply
#2
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.
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
I tried all options but no type code is understanding character data.......
Reply
#5
(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.
Reply
#6
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting python to FileMaker DWolf 6 1,600 Sep-22-2022, 03:40 AM
Last Post: DWolf
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,779 May-13-2022, 03:44 PM
Last Post: deanhystad
  Need help converting string to int dedesssse 7 2,615 Jul-07-2021, 09:32 PM
Last Post: deanhystad
  Beautify dictionary without converting to string. sharoon 6 3,297 Apr-11-2021, 08:32 AM
Last Post: buran
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,576 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,958 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  how to deal with problem of converting string to int usthbstar 1 1,931 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Converting string to hex triplet menator01 4 4,217 Aug-03-2020, 01:00 PM
Last Post: deanhystad
  Create array from string steve87bg 4 3,114 Jul-13-2020, 07:55 PM
Last Post: jefsummers
  Make an array of string number in a List polantas 5 3,029 May-27-2020, 07:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020