to convert <class 'bytes'> data in tuple in python? example:
data = b'["1","2","3"]'
data = tuple(data)
print(data)
output:(91, 34, 49, 34, 44, 34, 50, 34, 44, 34, 51, 34, 93)
**but i need to output like bellow**
expected output: data = (1,2,3)
CodePudding user response:
One approach:
from ast import literal_eval
data = b'["1","2","3"]'
res = literal_eval(data.decode("utf-8"))
print(res)
CodePudding user response:
We don't know how is encoded
the original string of bytes.
Suppose they are in json:
import json
tuple(json.loads(data))
#('1', '2', '3')
If they (unfortunately) are a py representation:
tuple(eval(data))
#('1', '2', '3')
The main question is, how are they encoded in a string?