In Python 3 I'm getting error TypeError: a bytes-like object is required, not 'bytearray'

I have a bytearray, which looks like this:

 >>> print(my_ba)
 bytearray(b'}\x0e\x15/ow4|-')

If I enter this in the console it works:

 >>> print(base58.b58encode(b'}\x0e\x15/ow4|-'))
 2bKmhuGiGP7t8

But this gives an error, and I can't find out how to get the b'' string from the bytearray:

 >>> print(base58.b58encode(my_ba))
 TypeError: a bytes-like object is required, not 'bytearray'

I'm sure it's obvious, but how do I convert the bytearray to a string with a b prefix?

推荐答案

As Coldspeed put it in the comments, just pass a bytearray to a bytes call:

bytes(my_ba)

Python-3.x相关问答推荐

错误2没有这样的文件或目录website_content.txt""

Gekko优化超出了方程式的界限(由于某种原因,会产生变量)

使用递归将int转换为字符串

regexp多重前瞻行为的解释

Python GUI:tkinter应用程序作为Windows的实时桌面

msg-seviri l1.5本机文件

在不改变 python 中原始数组顺序的情况下,对多维字符串数组进行降序排序?

将数据框中的值与另一个数据框中的多列进行比较,以获取条目以有效方式匹配的列表列表

有没有一种方法可以通过输入从 0 到 255 的 R、G 和 B 值来生成 RGB colored颜色 ,而无需使用 python 中的 matplotlib 模块?

提高时间复杂度的一些建议

多进程:两个进程,一起杀死

`pyspark mllib` 与 `pyspark ml` 包

如何从脚本中提取 PDF 文档的标题以进行重命名?

Django 2 个字段之一不能为空

在 Python 3.5 中使用 aiohttp 获取多个 url

为什么我在 Python 中收到错误消息无法导入名称 NoneType?

__new__ 方法给出错误 object.__new__() 只接受一个参数(要实例化的类型)

Python中的依赖倒置

如何等待 create_task() 创建的任务完成?

将列表列表转换为Python中的字典字典