首页 > 解决方案 > 当我在 Python3 中使用默认获取时,如何获得预期的 str 实例,NoneType 发现错误?

问题描述

我看到这个错误:

data["source_domain"], data["ip_address"]])
TypeError: sequence item 3: expected str instance, NoneType found

但这是我导致该错误的代码:

    data["segment_user_id"] = record.get("userId", "")
    traits = record.get("traits", {})
    data.update({
        # country_name -> country
        trait.replace("country_name", "country"): traits.get(trait, "")
        for trait in ["first_name", "last_name", "email", "country_name", "city", "ip_address"]
    })

    # Add composite key
    to_hash = ''.join([data["project_id"], data["anonymous_id"], data["source_system"], data["segment_user_id"],
                       data["source_domain"], data["ip_address"]])
    data["message_composite_key"] = hashlib.md5(to_hash.encode()).hexdigest()

看起来序列号 3data["segment_user_id"]是无。但这怎么可能呢?我的record.get("userId", "")代码不是确保始终将其设置为空字符串吗?

标签: python-3.x

解决方案


推荐阅读