Menace
Menace

Reputation: 227

Base58 and utf-8 Encoding Issues in Python 3.6.5

I'm using Python 3.6.5.

Hello, I'm having issues with encoding. I believe the issue has something to do with trying to use both base58 and utf-8. This portion of the program worked in Python 2.7.14 and I'm trying to convert it to Python 3.6.5.

def save_asset(asset):
nameString = '{0}_!_{1}'.format(asset['Name'].encode('utf-8').strip(), asset['AssetTypeID'])
filename = base58.b58encode(nameString)+'.png'

Cmd output:

File "Transfer.py", line 315, in start_download
    saveAttempt = save_asset(a)
File "Transfer.py", line 221, in save_asset
    filename = base58.b58encode(nameString)+'.png'
TypeError: can't concat str to bytes

Upvotes: 1

Views: 1810

Answers (2)

Morse
Morse

Reputation: 9144

base58.b58encode(nameString) is of bytes type. You can not concatenate string '.png' with bytes type.

You may use like below

filename = base58.b58encode(nameString)+base58.b58encode('.png')
print(filename)
>>b'blah-blah-byte-string'

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

Maybe b58encode (from whatever library you're using) works in the same peculiar way as b64encode from the standard library and returns a byte array, not a text string.

If you want your filename to be a byte array, use b".png" instead. If you want it to be a text string, decode the bytes returned by b58encode by using .decode("ascii") (many other encodings should also work, but that one is efficient).

Upvotes: 1

Related Questions