Encoding is new to me in Python, and I am trying to understand it. Apologies if this has been asked and answered already.
I am trying to encode a Python list and decode it. When I am trying to encode a list directly, I am hitting an error like below.
>>> my_list = [1, 2, 3]
>>> encoded_list = base64.b64encode(my_list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 54, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
TypeError: b2a_base64() argument 1 must be string or buffer, not list
To fix it, I converted the list object to a string and passed it to the encode function and I was able to successfully encode it.
>>> encoded_list = base64.b64encode(str(my_list))
>>> encoded_list
'WzEsIDIsIDNd'
When I try to decode it, I get a decoded string like below.
>>> decoded_list = base64.b64decode(encoded_list)
>>> decoded_list
'[1, 2, 3]'
>>> type(decoded_list)
<type 'str'>
But my original intention was to encode and decode a list and not convert the list to a string and then string to list.
Pretty sure this is not the right way to encode objects like dict or a list. If that's the case, Can someone please enlighten me on how to encode/decode non string objects in Python?
Thanks very much.
Try encoding/decoding using JSON instead of string.
import json
import base64
my_list = [1, 2, 3]
json_encoded_list = json.dumps(my_list)
#: '[1, 2, 3]'
b64_encoded_list = base64.b64encode(json_encoded_list)
#: 'WzEsIDIsIDNd'
decoded_list = base64.b64decode(b64_encoded_list)
#: '[1, 2, 3]'
my_list_again = json.loads(decoded_list)
#: [1, 2, 3]
But in practice, for pretty much any storage reasons I can think of there's no real reason to base64 encode your json output. Just encode and decode to json.
my_list = [1, 2, 3]
json_encoded_list = json.dumps(my_list)
#: '[1, 2, 3]'
my_list_again = json.loads(json_encoded_list)
#: [1, 2, 3]
If you need anything more complicated than Arrays and Dictionaries, then probably go with 7stud's pickle method. However JSON is simple, readable, widely supported and cross-compatible with other languages. I'd choose it whenever possible.