How not to throw Python exception when converting an integer string to int

I have some HTML that I am trying to parse. There are times when html attributes alone will not help me determine the type of the row (title and data). Luckily, if my string is a data string, then it must have some values ​​that can be converted to integers. I figured out how to convert unicode to integer for those cases where conversion can be done. I am struggling to write logic to go past the cells that the transform will not work because the cell has content that needs to be processed as text.

for example, if rowColumn [1] [3] can be converted to an integer, I can do it with

int(rowColumn[1][3].replace(',','').strip('$'))

      

but I get an error if the string Column [1] [3] has text content.

+1


source to share


1 answer


Have you looked at the try expression?



try:
    x = int(rowColumn[1][3].replace(',','').strip('$'))
except ValueError, e:
    x = None # rowColumn[1][3] was not an integer

      

+4


source







All Articles