TypeError: Unicode-objects must be encoded before hashing in Hashlib Function

TypeError: Unicode-objects must be encoded before hashing in Hashlib Function

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

Link 到存储库: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

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

也许尝试在 hashlib 命令中编码:

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

这有效

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