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
#2
I'm trying this out. In the output from fromstring, I added the following
    imageStr = str(parts[4])
    imageStr = imageStr.replace('[','')
    imageStr = imageStr.replace(']','')
    imageArr = np.fromstring(imageStr, dtype=int, sep=' ')
    print("[x] imageArr type %s" % type(imageArr))
    print("[x] imageArr ", imageArr)
And this is now the output:
Output:
[*] Waiting for messages. To exit press CTRL+C [x] message '0,1617735054,1617735054620256300,4,[104 126 180 87]' [x] message type <class 'str'> [x] parts ['0', '1617735054', '1617735054620256300', '4', '[104 126 180 87]'] [x] parts len 5 [x] imageStr 104 126 180 87 [x] imageStr type <class 'str'> [x] imageArr type <class 'numpy.ndarray'> [x] imageArr [104 126 180 87]
Looks like the '[]' had to be removed from the string, and Numpy puts them back when converting to the int array.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numpy, array(2d,bool), flipping regions. MvGulik 2 982 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,054 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  ValueError: could not broadcast input array from shape makingwithheld 1 2,321 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  python code to calculate mean of an array of numbers using numpy viren 3 1,174 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Writing a cycle to find the nearest point from the array Tysrusko 0 773 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 2,583 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 1,329 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 9,053 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 992 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 1,253 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