laspal
laspal

Reputation: 3557

best way to find out type

I have a dict

val_dict - {'val1': 'abcd', 'val': '1234', 'val3': '1234.00', 'val4': '1abcd 2gfff'}

All the values to my keys are string.

So my question is how to find out type for my values in the dict.

I mean if i say`int(val_dict['val1']) will give me error.

Basically what I am trying to do is find out if the string is actual string or int or float.`

if int( val_dict['val1'):
dosomething
else if float(val_dict['val1']):
dosomething

thanks

Upvotes: 1

Views: 370

Answers (4)

lunixbochs
lunixbochs

Reputation: 22415

you can determine if the string will convert to an int or float very easily, without using exceptions

# string has nothing but digits, so it's an int
if string.isdigit():
    int(string)

# string has nothing but digits and one decimal point, so it's a float
elif string.replace('.', '', 1).isdigit():
    float(string)

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 882691

All the values are of course "actual strings" (you can do with them all you can possibly do with strings!), but I think most respondents know what you mean -- you want to try converting each value to several possible types in turn ('int' then 'float' is specifically what you name, but couldn't there be others...?) and return and use the first conversion that succeeds.

This is of course best encapsulated in a function, away from your application logic. If the best match for your needs is just to do the conversion and return the "best converted value" (and they'll all be used similarly), then:

def best_convert(s, types=(int, float)):
  for t in types:
    try: return t(s)
    except ValueError: continue
  return s

if you want to do something different in each case, then:

def dispatch(s, defaultfun, typesandfuns):
  for t, f in typesandfuns:
    try: 
      v = t(s)
    except ValueError:
      continue
    else:
      return f(v)
  return defaultfun(s)

to be called, e.g, as

r = dispatch(s, asstring, ((int, asint), (float, asfloat)))

if the functions to be called on "nonconvertible strings", ones convertible to int, and ones convertible to float but not int, are respectively asstring, asint, asfloat.

I do not recommend putting the "structural" "try converting to these various types in turn and act accordingly" code in an inextricable mixture with your "application logic" -- this is a clear case for neatly layering the two aspects, with good structure and separation.

Upvotes: 2

eswald
eswald

Reputation: 8406

A simple solution, if you don't have too many formats, could involve checking the format of each value.

def intlike(value):
    return value.isdigit()
def floatlike(value):
    import re
    return re.match("^\d+\.\d+$")

if intlike(val_dict['val1']):
    dosomething(int(val_dict['val1']))
elif floatlike(val_dict['val1']):
    somethingelse(float(val_dict['val1']))
else:
    entirelydifferent()

However, it really is easier to use Python's exception framework for certain complex formats:

def floatlike(value):
    try:
        float(value)
    except ValueError:
        result = False
    else:
        result = True
    return result

Upvotes: -1

user319799
user319799

Reputation:

Maybe this:

is_int = True
try:
    as_int = int (val_dict['val1'])
except ValueError:
    is_int = False
    as_float = float (val_dict['val1'])

if is_int:
    ...
else:
    ...

You can get rid of is_int, but then there will be a lot of code (all float value handling) in try...except and I'd feel uneasy about that.

Upvotes: 3

Related Questions