TypeError:必须先对Unicode对象进行编码,然后再在Hashlib函数[SOLVED]中进行散列

问题描述 投票:3回答:2

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

链接到资源库:https://github.com/executable16/audio-fingerprint-identifying-python

然而,错误具体在行号。 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
2个回答
3
投票

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

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

1
投票

此有效

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

© www.soinside.com 2019 - 2024. All rights reserved.