【问题标题】:Deterministic hashing of Python 3 strings with adler32使用 adler32 对 Python 3 字符串进行确定性散列
【发布时间】:2015-06-01 22:11:30
【问题描述】:

我读到here 如下:

注意: 在所有 Python 版本中生成相同的数值,并且 平台使用adler32(data) & 0xffffffff

我希望将其应用于表单的字符串:"S89234IX",但是当我这样做时,我得到:

> zlib.adler32("S89234IX")

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-84eee14d45ae> in <module>()
----> 1 zlib.adler32(campaigns_to_work_with[0])

TypeError: 'str' does not support the buffer interface

关于如何将此函数应用于字符串的任何想法?

【问题讨论】:

  • 试试"S89234IX".encode('utf-8')

标签: python python-3.x hash zlib


【解决方案1】:

data 必须是字节串。如果要计算 Unicode 数据的校验和,则需要将其编码为字节字符串,并且需要确保使用特定的编码。例如,使用 UTF-8:

checksum = zlib.adler32("S89234IX".encode('utf-8')) & 0xffffffff

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2012-11-26
    • 2020-03-07
    • 2011-01-08
    • 1970-01-01
    • 2021-10-13
    • 2022-12-05
    相关资源
    最近更新 更多