首页 > 解决方案 > Python - 应该将哪种编码应用于要传递给 hashlib.sha256() 的字符串?

问题描述

首先我在 pyspark udf 中使用了这个函数,它工作正常

def encrypt_hash_col_value(column):
    sha_value = hashlib.sha256(column.encode()).hexdigest()
    return sha_value

spark_udf = udf(encrypt_hash_col_value, StringType())

df_hashed = df.withColumn("metric_1", spark_udf("metric_1"))

但是pandas_udf首先我得到一个错误: AttributeError: 'Series' object has no attribute 'encode' 我通过修改行来解决这个问题hashlib.sha256(column.str.encode()).hexdigest()

但现在它失败了TypeError: encode() missing 1 required positional argument: 'encoding'。在这种情况下,应该将什么编码作为参数添加到 column.str.encode() ?

当我使用 column.str.encode("UTF-8") 时,出现此错误:TypeError: object supporting the buffer API required

标签: pythonpython-3.xpandasencoding

解决方案


推荐阅读