5

Is there any way to understand what data type that a string holds... The question is of little logic but see below cases

varname = '444'
somefunc(varname) => int

varname = 'somestring'
somefunc(varname) => String

varname = '1.2323'
somefunc(varname) => float

My Case: I get a mixed data in a list but they're in string format.

myList = ['1', '2', '1.2', 'string']

I'm looking for a generic way to understand whats their data so that i can add respective comparison. Since they're already converted to string format, I cant really call the list (myList) as mixed data... but still is there a way?

7
  • I tried type(eval('123')) => int, but type(eval('somename')) would fail as it cant be evaluated... also would work wrong if there is a variable named somename Commented Jul 12, 2013 at 21:57
  • 1
    If you have just a few types to test, like the basic data types and string, then yes (just try to cast them). Otherwise, no. Commented Jul 12, 2013 at 21:57
  • 1
    You should really edit your OP instead of commenting on it Commented Jul 12, 2013 at 21:58
  • @JonClements: most eval calls are inherently very dangerous, no? Commented Jul 12, 2013 at 21:58
  • It's limited to certain basic types and doesn't do execution - so will except rather than do anything untoward. In the case of the op this will be an unknown type or the value is left as.a string Commented Jul 12, 2013 at 22:01

3 Answers 3

15
from ast import literal_eval

def str_to_type(s):
    try:
        k=literal_eval(s)
        return type(k)
    except:
        return type(s)


l = ['444', '1.2', 'foo', '[1,2]', '[1']
for v in l:
    print str_to_type(v)

Output

<type 'int'>
<type 'float'>
<type 'str'>
<type 'list'>
<type 'str'>
Sign up to request clarification or add additional context in comments.

1 Comment

Will work for most "primitive" types. Won't work for anything more complex, such as trying to get the type of a variable which happens to be a function object (like a lambda).
8

You can use ast.literal_eval() and type():

import ast
stringy_value = '333'
try:
    the_type = type(ast.literal_eval(stringy_value))
except:
    the_type = type('string')

1 Comment

Glad to help, Prasath. Pretty much the same as perreal's answer.
1

I would just try different types, in the right order:

>>> def detect(s):
...     try:
...         return type(int(s))
...     except (TypeError, ValueError):
...         pass
...     try:
...         return type(float(s))
...     except (TypeError, ValueError):
...         pass
...     return type(s)
... 
>>> detect('3')
<type 'int'>
>>> detect('3.4')
<type 'float'>
>>> detect('foo')
<type 'str'>

1 Comment

Caveat: You have to do int before float otherwise you could do float(1) and it'd be just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.