Вы пытались декодировать свои данные в неправильном порядке, вам нужно вернуться назад по сравнению с порядком кодирования:
import base64
words = ('word€',"word2") # Added some non-ascii characters for testing
for word in words:
# Encoding
print("Word:", word)
utf8_encoded = word.encode('utf8') # Encoding in utf8, gives a bytes object
print('utf8 encoded:', utf8_encoded)
str_encoded = base64.b64encode(utf8_encoded) # Encoding it in B64
print("Base64 encoded:", str_encoded)
# Decoding
b64_decoded = base64.standard_b64decode(str_encoded) # Decoding from B64, we get a bytes object
print("Decoded from base64:", b64_decoded)
str_decoded = b64_decoded.decode('utf-8') # and decode it (as utf8) to get a string
print("Decoded string:", str_decoded, '\n')
Выход:
Word: word€
utf8 encoded: b'word\xe2\x82\xac'
Base64 encoded: b'd29yZOKCrA=='
Decoded from base64: b'word\xe2\x82\xac'
Decoded string: word€
Word: word2
utf8 encoded: b'word2'
Base64 encoded: b'd29yZDI='
Decoded from base64: b'word2'
Decoded string: word2