'float' 对象没有属性 'strip'?

'float' object has no attribute 'strip'?

我有一个数据集,其中包含 reddit 的 NameAccount 以及他们用时间和 subreddit 编写的消息。像这样:

对于我的 porpuse,我需要一个包含 [name of account , all the messages that he has written] 的数组(因为正文(看图片)只有一条消息,但如果我们看到所有作者,就会有重复)。

所以我写了这个程序:

test_data = pd.read_csv("addres/test_data.csv", encoding="utf8")
test = test_data[['author', 'body']]
lista = [list(x) for x in test.values] 
test=dict()
for i in range(1107946):
    if lista[i][0] in test:
        test[lista[i][0]].append(lista[i][1])
    else:
        test[lista[i][0]]=[lista[i][1]]

然后我得到了我喜欢的东西。 如果我写 test["Name"] 我会得到那个人的所有消息。 例如:

test["ZenDragon"]

['At 7680 by 4320 with 64x AA, right?',  'Wrong subreddit for this kind of post, but /r/frugal and /r/lifeprotips might be interested.',  'This is something GravityBox can do. (a module for XPosed Framework)',etc]

现在我想加入所有这些行。 例如:["message1"、"message2"、"message3" 等..] -> ["message 1 message 2 etc..."] 我试过写这个东西:

for i in test.keys():
    X.append(" ".join(line.strip() for line in test[i]))

但是我有这个错误: 'float' 对象没有属性 'strip'

但是我没有浮动对象?

好吧,显然在你的 test 字典中存在一个键 i,其关联值是一个元素列表,其中至少有一个不是字符串,而是浮点数。

您可以将代码包装在 try-catch 中以帮助缩小问题的原因范围:

for i in test.keys():
    try:
        for line in test[i]:
            line.strip()
    except:
        print(i)
        print(line)