Python Forum
Convert String of an int array to a Numpy array of ints
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert String of an int array to a Numpy array of ints
#1
Hi

I am sending a Numpy int array over RabbitMQ and it seem to be receiving correctly.

This is the output RabbitMQ's publish that is sent:

>py msg_send_test.py
Output:
[x] Sent '0,1617731222,1617731222810382600,4,[ 48 251 9 205]'
This is the output on the receiving end from RabbitMQ's consumer:
>py msg_receive_test.py
Output:
[*] Waiting for messages. To exit press CTRL+C [x] message '0,1617732445,1617732445003573700,4,[209 227 205 72]' [x] message type <class 'str'> [x] parts ['0', '1617732445', '1617732445003573700', '4', '[209 227 205 72]'] [x] parts len 5 [x] imageStr [209 227 205 72] [x] imageStr type <class 'str'>
This is the section of code to produce the output from the RabbitMQ consumer:
    message = body.decode()
    parts = message.split(',')
    print("[x] message      %r" % message)
    print("[x] message type %s" % type(message))
    print("[x] parts        %s" % parts)
    print("[x] parts len    %s" % len(parts))
    imageStr = str(parts[4])
    print("[x] imageStr     %s" % imageStr)
    print("[x] imageStr type %s" % type(imageStr))
What I would like to do is convert the imageStr into a Numpy array of ints. The whole array is a string, not an array of individual strings.

Any suggestions would be appreciated.

Thanks...

When I try using Numpy's fromstring method:
    imageArr = np.fromstring(imageStr, dtype=float, sep=' ')
    print("[x] imageArr type %s" % type(imageArr))
    print("[x] ", imageArr)
it gives the following output:
Output:
[x] imageStr type <class 'str'> [x] imageArr type <class 'numpy.ndarray'> [x] []
Reply


Messages In This Thread
Convert String of an int array to a Numpy array of ints - by mdsousa - Apr-06-2021, 06:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 462 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 305 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,926 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 378 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 601 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,251 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 591 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 703 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,311 Jun-06-2023, 09:46 PM
Last Post: FXMonkey
  [Newbie] Multiple Array azhuda 3 1,041 Jun-01-2023, 04:29 AM
Last Post: azhuda

Forum Jump:

User Panel Messages

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