【问题标题】:'float' object has no attribute 'strip'?'float' 对象没有属性 'strip'?
【发布时间】:2020-03-25 19:28:50
【问题描述】:

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

对于我的porpuse,我需要一个带有[帐户名称,他写的所有消息]的数组(因为正文(看图片)只有一条消息,但是如果我们看到所有作者,就会出现重复) .

所以我写了这个程序:

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",etc..] -> ["message 1 message 2 etc..."] 我试过写这个东西:

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

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

但我没有浮动对象?

【问题讨论】:

  • 如果您遇到该错误,line 必须 在某些时候是一个浮点数。仔细检查您的数据。

标签: python arrays dictionary join strip


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-17
    • 2015-02-02
    • 2017-05-03
    • 2020-03-25
    • 2016-08-11
    • 2016-12-02
    • 2018-06-02
    • 2019-12-31
    相关资源
    最近更新 更多