Home > Blockchain >  Rectifying the numpy string
Rectifying the numpy string

Time:07-01

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.

CodePudding user response:

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

CodePudding user response:

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()
  • Related