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
  Numpy, array(2d,bool), flipping regions. MvGulik 2 946 Oct-27-2024, 11:06 AM
Last Post: MvGulik
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,018 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  ValueError: could not broadcast input array from shape makingwithheld 1 2,147 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  python code to calculate mean of an array of numbers using numpy viren 3 1,111 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Writing a cycle to find the nearest point from the array Tysrusko 0 749 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 2,513 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 1,299 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 8,878 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 967 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 1,238 Nov-28-2023, 06:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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