TypeError:'in <string>'需要将字符串作为左操作数,而不是浮点数

时间:2018-10-12 20:34:32

标签: string python-3.x loops

我有使用伪造日期的代码(如下),它工作正常:

exg = ["I love apple.", 
        "there are lots of health benefits of apple.", 
        "apple is especially hight in Vitamin C,", 
        "alos provide Vitamin A as a powerful antioxidant!"]


fruit_list = ["pear", "banana", "mongo", "blueberry", "kiwi", "apple", "orange"]

for j in range(0, len(exg)):
    sentence = exg[j]
    if any(word in sentence for word in fruit_list):
        print(sentence)

输出如下:仅显示在fruit_list中包含单词的句子。

I love apple.
there are lots of health benefits of apple.
apple is especially hight in Vitamin C,

然后,我将fruit_list更改为我的真实数据(real_list),真实数据来自excel工作表中的一列。读取数据的代码如下:

import pandas as pd

data = pd.read_excel('C:/Users/my/Desktop/my_list.xlsx', 'Sheet1')
real_list = data['name'].tolist()

但是我的代码不再起作用,并且错误显示如下:

  ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-101-5a886e386099> in <module>()
      7 for j in range(0, len(exg)):
      8     sentence = exg[j]
----> 9     if any(word in sentence for word in real_list):
     10         print(sentence)

<ipython-input-101-5a886e386099> in <genexpr>(.0)
      7 for j in range(0, len(exg)):
      8     sentence = exg[j]
----> 9     if any(word in sentence for word in real_list):
     10         print(sentence)

TypeError: 'in <string>' requires string as left operand, not float

我确定问题出在real_list。而且我无法在此处显示real_list(希望您理解),但是以前有人遇到过此错误消息吗?知道是什么原因吗?请发送帮助。非常感谢!

1 个答案:

答案 0 :(得分:0)

已修复:

real_list中有一个“ N / A”,是浮点数。

感谢您的所有评论!