【无标题】TypeError: ‘>‘ not supported between instances of ‘str‘ and ‘int‘

for row in sh.rows:
num = row[1].value
print(num)
>>>2.6
明明结果打印是数字
然而,当我添加了一行时:
for row in sh.rows:
num = row[1].value
if num > 4:
data1.append(row)
else:
data2.append(row)
>>>TypeError: '>' not supported between instances of 'str' and 'int'
却报错了,提示我两边不能进行str和int类型的比较
但是它明明不是str类型的啊
此时我用了一下eval函数,却发现解决了问题
if eval(num) > 4:
程序成功的运行了~

【无标题】TypeError: ‘>‘ not supported between instances of ‘str‘ and ‘int‘最先出现在Python成神之路

版权声明:
作者:倾城
链接:https://www.techfm.club/p/14675.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>