Python: Need help converting a string of hex values into numpy array - python

I am very much a Python beginner using Thonny and Python 3.7.7. I have strings of values that I want to convert to integers and put in a numpy array. A typical string:
print(temp)
05:01:00016043:00002F4F:00002F53:00004231:000050AA:00003ACE:00005C44:00003D3B:000064BC
temp = temp.split(":")
print(temp)
['05', '01', '00016043', '00002F4F', '00002F53', '00004231', '000050AA', '00003ACE', '00005C44', '00003D3B', '000064BC']
I want to efficiently turn this list of strings describing hexadecimal numbers into integers and put them into an numpy array (with the emphasis on efficiently!).
a = np.array([11], dtype=int)
Any suggestions? Thanks

How about a nice and tidy one-line list comprehension? For a string s:
np.array([int(hexa, base=16) for hexa in s.split(sep=":")])
This may look complicated, but the output of s.split(sep=":") is a list of string hexadecimals. Passing each one of them (each hexa) into int with base=16 converts them, as you'd like.

Apply function which converts x to int(x, 16) to every element in L
import pandas as pd
import numpy as np
L = ['05', '01', '00016043', '00002F4F', '00002F53', '00004231', '000050AA', '00003ACE', '00005C44', '00003D3B', '000064BC']
output = pd.Series(L).apply(lambda x:int(x, 16)).values
print(output)
output is
[ 5 1 90179 12111 12115 16945 20650 15054 23620 15675 25788]

Related

Rectifying the numpy string

I am doing automation on the manual work where I am reading the data from outlook mail and storing the required data in a NumPy string array. However, data having lots of space you say dummy one. I need to rectify the NumPy string.
import numpy as np
arr=np.array([])
#outlook code and store in array.
arr=[{'5'} {'9'} {'7'} {'9'} {''} {''} {''} {''} {''} {''}]
# required output look like this
arr=[5979]
Can anyone help to get me the required output.
Solution for this given format but not scalable.
It iterates over each set contained in the list and unpack them to another list of string. Then is convert the list of string to a single string and finally to an integer
arr = [{'5'}, {'9'}, {'7'}, {'9'}, {''}, {''}]
value = int("".join([str(*x) for x in arr if str(*x).isdigit()]))
print(value)
5979
You can .strip() each string to remove spaces and append to previous strings. I'm not sure why you use sets inside a list and not strings directly, this will save you that next(iter(..)). Also, note that you won't get much benefit from numpy array of strings, but for numeric arrays you can get huge benefits.
arr = [{'5'}, {'9'}, {'7'}, {'9'}, {' '}, {' '}, {' '}]
value = ''
for s in arr:
value += next(iter(s)).strip()

How to Convert a List to numpy.datetime64 format

I know that we can create a single string to np.datetime64 format such as:
a = np.datetime64('2020-01-01')
But what if we have a list with multiple strings of dates in it?
How are we able to apply the same np.datetime64 to convert all the elements inside into a datetime format? Apart from doing a for-loop perhaps.
When you have your string list, use it as a source to a Numpy array,
passing datetime64 as dtype. E.g.:
lst = ['2020-01-01', '2020-02-05', '2020-03-07' ]
a = np.array(lst, dtype='datetime64')
When you execute a (actually print this array in a notebook),
you will get:
array(['2020-01-01', '2020-02-05', '2020-03-07'], dtype='datetime64[D]')
As you can see, in this case the default precision is Day.
But you can pass the precision explicitely, e.g. b = np.array(lst, dtype='datetime64[s]').
Don't be misled by apostrophes surrounding each element in the above
printout, they are not strings. To check it, execute a[0] and
you will get:
numpy.datetime64('2020-01-01')
Using list comprehension:
strings_list= [...]
npdate_list = [np.datetime64(x) for x in strings_list]
Is there a specific reason for you to want to avoid a loop?
List comprehension is okay?

Assigning string to a list

import random
import numpy as np
LOC = np.zeros(96)
LOC[0] = 'H'
for t in range(0,96):
if 32<t<40:
LOC[t] = random.choice(['H','W','I'])
Here, I want to initialize LOC with the character 'H' and has the check few conditions. But when I try to assign it, I am getting an error could not convert string to float: 'H'. How can I assign a character/string to the list LOC?
NumPy is not really made for mixing types of content. If you want an array of strings the empty values shouldn't be zero, but rather empty strings: ''
You can use random.choices() to get the random values and assign, but the trick is to set the dtype to something that's appropriate for strings::
import random
import numpy as np
LOC = np.zeros(96, dtype='<U1')
LOC[0] = 'H'
LOC[32:40] = random.choices(['H','W','I'], k = 40 - 32)
This will be an array of empty strings except where you've assigned random values. Regular python lists, of course work with mixed types, if you don't need NumPy, you can initialize the array with:
LOC = [0] * 96
and then proceed with setting values with whatever you want.
In python u can use the ord function to get the unicode code point of a charachter. So using
LOC[t] = ord(random.choice(['H','W','I']))
you should be able to achieve your goal, even though I would call it 'assigning a character to a numpy array' and not 'assigning a string to a list'.

Numpy: How to cast string array to float while some of the elements contain extra quoted string

I am trying to convert string to float type by the following
X = arr[:,:-1].astype(np.float32)
However, error as below is rising
ValueError: could not convert string to float: '"53"'
I know this means I have some elements with extra quote in the array.
My problem is how should I solve this. How can I convert element '"53"' into 53 inside the array?
UPDATE 1:
Here is an example to reproduce
import numpy as np
a = np.array([['12','13'],['"53"','44']])
a = a.astype(np.float32)
Try stripping the double quotes from the array, then casting to float.
Like so:
arr = np.char.strip(arr, '"')
X = arr[:,:-1].astype(np.float32)
You could also use numpy.char.replace() to perform element-wise string replace on an array of strings.
Signature: np.char.replace(a, old, new, count=None)
Docstring: For
each element in a, return a copy of the string with all occurrences
of substring old replaced by new. Calls str.replace element-wise.
import numpy as np
a = np.array([["12","13"],['"53"',"44"]])
b = np.char.replace(a, '"', '')
c = b.astype(np.float32)

python Hex string to array

I have a string
s='0xbb06e6cf,0xbb6fceb1,0xbabb39c3'
and first I want to convert it to array like
arr = [0xbb06e6cf,0xbb6fceb1,0xbabb39c3]
and then change the arr to float64 arr, which is the fastest way to convert the hex arr to float64?
You can do it using below code:
s='0xbb06e6cf,0xbb6fceb1,0xbabb39c3'
x=s.split(",")
print x
Output is:
['0xbb06e6cf', '0xbb6fceb1', '0xbabb39c3']
First you want to split the string on "," characters. Python implements a split() function that does this. Then you would want to convert each string returned from the split function into a number, you can use the int() function for this by specifying the base the number is in. Using list comprehension, the following code will do what you want:
s='0xbb06e6cf,0xbb6fceb1,0xbabb39c3'
arr = [int(n, base=16) for n in s.split(',')]

Categories

Resources