【问题标题】:TypeError: Unicode-objects must be encoded before hashing in Hashlib FunctionTypeError:Unicode对象必须在Hashlib函数中散列之前进行编码
【发布时间】:2021-03-29 02:33:00
【问题描述】:

我已经在 stackoverflow 上检查了同一问题的所有其他解决方案,并尝试了它们,但似乎没有任何效果。我只是在这里发布链接而不是代码,因为代码很大而且交互性较差。

存储库链接:https://github.com/executable16/audio-fingerprint-identifying-python

但具体来说,错误出现在第 1 行。 2 这里:

if t_delta >= MIN_HASH_TIME_DELTA and t_delta <= MAX_HASH_TIME_DELTA:
            h = hashlib.sha1("%s|%s|%s" % (str(freq1), str(freq2), str(t_delta)))
            yield (h.hexdigest()[0:FINGERPRINT_REDUCTION], t1)

我尝试使用 .encode('utf-8') 但没有帮助。这是我尝试过的:

if t_delta >= MIN_HASH_TIME_DELTA and t_delta <= MAX_HASH_TIME_DELTA:
            first = str(freq1).encode('utf-8')
            second = str(freq2).encode('utf-8')
            third = str(t_delta).encode('utf-8')
            h = hashlib.sha1("%s|%s|%s" % (first, second, third))
            yield (h.hexdigest()[0:FINGERPRINT_REDUCTION], t1)

文本形式错误:

sqlite - connection opened
 * id=2 channels=2: file_example_MP3_700KB.mp3
   new song, going to analyze..
   fingerprinting channel 1/2
   local_maxima: 664 of frequency & time pairs
Traceback (most recent call last):
  File "collect-fingerprints-of-songs.py", line 54, in <module>
    channel_hashes = set(channel_hashes)
  File "/home/executable/Desktop/audio-fingerprint-identifying-python/libs/fingerprint.py", line 168, in generate_hashes
    h = hashlib.sha1("%s|%s|%s" % (str(freq1), str(freq2), str(t_delta)))
TypeError: Unicode-objects must be encoded before hashing
sqlite - connection has been closed
make: *** [Makefile:19: fingerprint-songs] Error 1

如果我能找到一些可行的解决方案以及适当的解释,那就太好了。

【问题讨论】:

    标签: python python-3.x utf hashlib audio-fingerprinting


    【解决方案1】:

    也许可以尝试在hashlib 命令中进行编码:

    h = hashlib.sha1("%b|%b|%b".encode('utf-8') % (str(freq1), str(freq2), str(t_delta)))
    

    【讨论】:

    【解决方案2】:

    成功了

    h = hashlib.sha1(b"%s|%s|%s" % (str(freq1).encode('utf-8'), str(freq2).encode('utf-8'), str(t_delta).encode('utf-8')))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多