How do I not raise a Python exception when converting an integer-as-string to an int
I have some HTML I am trying to parse. There are cases where the html attributes alone are not going to help me identify the row type (header versus data). Fortunately, if my row is a data row then it should have some values that can be converted to integers. I have figured out how to convert the unicode to an integer for those cases that it is possible to make the conversion. I am struggling to write the logic to move past the cells that the conversion will not work because the cell has content that must be treated as text.
for example if rowColumn can be converted to an integer I can do so by
but I get an error if rowColumn has text content.
Asked by: Chloe898 | Posted: 28-01-2022
Have you looked at the try statement?
Answered by: Rafael346 | Posted: 01-03-2022
try: x = int(rowColumn.replace(',','').strip('$')) except ValueError, e: x = None # rowColumn was not an integer
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python