python int()转换出错抛出异常类型_将整数字符串转换为int时如何不引发Python异常...

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[1][3] can be converted to an integer I can do so by

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

but I get an error if rowColumn[1][3] has text content.

解决方案

Have you looked at the try statement?

try:

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

except ValueError, e:

x = None # rowColumn[1][3] was not an integer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值