Leo Leung
Leo Leung

Reputation: 81

Convert a byte list to python list

here is my task description, as I need to pass some data to the GCP cloud task HTTP request body, I need to convert a list to bytes list(as it is only accept bye data type), then convert back into a python list for the GCP cloud function. Right now I am having trouble converting a byte list back to a python list. Here is my code. I use the join method to join all the characters but it is still displayed separately.

dataset_id='ICN444499559951515151'

name='testing_1'

payload=[dataset_id,name]

byte_list=str(payload).encode()

x=byte_list.decode("utf-8")

y=reduce(concat,x)

print(list(x.join()))

but it still cannot join as a list as I wish. I want the result to be like this ["ICN444499559951515151","testing1"] and able to extract data for the cloud function.

Upvotes: 0

Views: 1325

Answers (2)

snakecharmerb
snakecharmerb

Reputation: 55630

str(payload) converts the list to a str, and this is not an easily reversible operation.

A better approach is to use the json module to serialise and deserialise the list and its elements.

>>> import json
>>> dataset_id='ICN444499559951515151'
>>> name='testing_1'
>>> payload=[dataset_id,name]
>>> serialised = json.dumps(payload)
>>> serialised
'["ICN444499559951515151", "testing_1"]'
>>> byte_list = serialised.encode()
>>> y = byte_list.decode("utf-8")
>>> x = json.loads(y)
>>> x
['ICN444499559951515151', 'testing_1']
>>> 

JSON is a widely used standard for serialising and deserialising data sent over http (and other protocols).

Upvotes: 4

AnsFourtyTwo
AnsFourtyTwo

Reputation: 2518

Your variable byte_list isn't actually a list. It is a byte string representing a list. Similarly, x is a string, not a list.

To convert the string back to a list, you have to apply some logic on how this should happens, as there is no standard conversion from string to list.

This can be done in one line with a list comprehension:

[e.strip().replace("'","") for e in x.strip("[]").split(",")]
# ['ICN444499559951515151', 'testing_1']

The above code takes x removes the surrounding brackets (strip("[]") and splits the string at ´,´ (split(","). This results in a list of strings, but still with some disturbing single quotes and spaces. Therefore, for each of those elements e, the code removes surrounding empty spaces (strip()) and replaces single quotes with empty strings (replace("'","").

Upvotes: 0

Related Questions