【python】避免读取excel时的TypeError: unsupported operand type(s) for +: ‘float‘ and ‘str‘错误

这个实际上是excel单元格为空的时候返回值为float类型的nan导致的错误。因为遇到的次数比较多,所以单独总结一下开一篇文章。

解决方法比较简单,主要是找到一个适用于字符型输入的nan判断函数。可以采用pandas库的isnull函数。

import pandas as pd
if pd.isnull(r1):
    r1 = ""

或者


from pandas import isnull
if isnull(r1):
    r1 = ""

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/31cc5a999d.html