1

I am running python 3.5 and trying to convert a large string of strings into a numpy array.

I used the answer from stack overflow to put this code together.

import ast
import numpy as np
str = '["8.4","4.3E-7"]'
arr = ast.literal_eval(str)
x = np.array(arr, dtype='|S4')
y = x.astype(np.float32)

I am getting the folllowing value error

ValueError: could not convert string to float: '4.3E'

str is an example string , most of the numbers don't have E-something pattern, but occasionally this happens and my code hangs up.

is there a way to convert string to numpy array in this case?

akhileshsk
  • 13
  • 3
  • 1
    `|S4` is a max length of 4 characters. You didn't give enough room. – user2357112 Jun 30 '17 at 16:51
  • You don't need to specify the dtype at all, though. It'll be inferred for you. (I'll go edit that other answer to clarify things.) – user2357112 Jun 30 '17 at 16:53
  • @user2357112 if u just post an answer, Ill be able to place accept officially. thanks for the answer though... turns out there is a lot to learn from this community :) – akhileshsk Jun 30 '17 at 16:56

1 Answers1

2

Your input contains a 6 character long string representation of a number (4.3E-7) but you tell numpy to import only upto 4 characters. Change the dtype='|S4' to dtype='|S6', or indeed remove it entirely and let numpy figure out the length of the strings. Also, make sure to avoid overwriting str to avoid headaches down the line!

import ast
import numpy as np
s = '["8.4","4.3E-7"]' # don't overwrite str!
arr = ast.literal_eval(s)
x = np.array(arr) # changed length
y = x.astype(np.float32)
Dom Weldon
  • 1,728
  • 1
  • 12
  • 24
  • It's simpler to just remove the dtype specifier entirely and let NumPy figure out how much room the strings need. – user2357112 Jun 30 '17 at 17:00