Search code examples
pythonstringint

Differentiating Python variables as str or int


I have a file that has 3 values on each line. It is a fairly random file, and any of these values can be str or int.

George, 34s, Nikon

42, absent, Alan

apple, 111, 41

marked, 15, never

...

So, I read in the line, and using split I get the first value:

theFile = r"C:\... "

tDC = open(theFile, "r")

for theLine in tDC:

        a, b, c = theLine.split(',')

So far so good.

Where I'm stuck is when I try to deal with variable a. I need to deal with it differently if it is a str or if it is an int. I tried setting a = int(a), but if it is a string (e.g., 'George') then I get an error. I tried if type(a) = int or if isinstance(a,int), but neither work because all the values come in as a string!

So, how do I evaluate the value NOT looking at its assigned 'type'? Specifically, I want to read all the a's and find the maximum value of all the numbers (they'll be integers, but could be large -- six digits, perhaps).

Is there a way to read in the line so that numbers come in as numbers and strings come in as strings, or perhaps there is a way to evaluate the value itself without looking at the type?


Solution

  • The first point is that you need some rule that tells you which values are integers and which ones aren't. In a data set that includes things like 32s, I'm not sure it makes sense to just treat anything that could be an integer as if it were.

    But, for simplicity, let's assume that is the rule you want: anything that could be an integer is. So, int(a) is already pretty close; the only issue is that it can fail. What do you do with that?

    Python is designed around EAFP: it's Easier to Ask Forgiveness than Permission. Try something, and then deal with the fact that it might fail. As Cyber suggests, with a try statement:

    try:
        intvalue = int(a)
    except ValueError:
        # Oops, it wasn't an int, and that's fine
        pass
    else:
        # It was an int, and now we have the int value
        maxvalue = max(maxvalue, intvalue)