Python 3编码或没有字符串参数的错误 - python

我在Python 2中有一个函数,该函数过去曾从UUID生成22个长度的随机字符串...。

def make_base64_string():
    return uuid.uuid4().bytes.encode("base64")[:22]

从那以后,我开始在Python 3中进行测试,并刚刚看完Pragmatic Unicode presentation,其中大部分都超出了我的脑海。无论如何,我不认为该函数现在可以在Python 3.4中运行,而且我是对的...。

因此,接下来,我尝试了希望解决方案,使低估状态下的bytes.encode消失了(将所有内容作为一个字节处理,对吗?)。

base64.b64encode(bytes(uuid.uuid4(), 'utf-8'))

但这给了我以下错误...

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding or errors without a string argument 

为什么?我想了解更多。

python大神给出的解决方案

base64.b64encode以字节为参数,无需解码-重新编码。

>>> base64.b64encode(uuid.uuid4().bytes)
b'58jvz9F7QXaulSScqus0NA=='
>>> base64.b64encode(uuid.uuid4().bytes)
b'gLV2vn/1RMSSckMd647jUg=='