I have a variable that contains the string ‘long’. How can I create a numpy dtype object with some type equivalent to long from this string? I have a file with many numbers and the corresponding types. int, float etc. are no problems, only long doesn’t work. I don’t want to hardcode some replacement long -> int32 or so in my code.
>>> np.dtype('long')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: data type not understood
Also: Is there a way to create a variable type from a string in pure python? I mean, I want the inverse of int.__name__, which converts the type name into a string.
In this particular case, I think you can use
getattrto get it from thenumpymodule itself:but: