如何将<class 'bytes'>元组中的数据转换成python?
How to convert <class 'bytes'> data in tuple in python?
要转换 python 中的 元组数据?
示例:
data = b'["1","2","3"]'
data = tuple(data)
print(data)
output:(91, 34, 49, 34, 44, 34, 50, 34, 44, 34, 51, 34, 93)
但我需要如下输出。
预期输出:
data = (1,2,3)
一种方法是先解码字节串,然后使用ast.literal_eval
将其转换为列表:
from ast import literal_eval
data = b'["1","2","3"]'
res = literal_eval(data.decode("utf-8"))
res = tuple(res)
print(res)
输出
('1', '2', '3')
我们不知道 encoded
原始字节串是怎样的。
假设他们在 json:
import json
tuple(json.loads(data))
#('1', '2', '3')
如果它们(不幸的是)是 py 表示:
tuple(eval(data))
#('1', '2', '3')
主要问题是,它们是如何编码成字符串的?
要转换 python 中的
data = b'["1","2","3"]'
data = tuple(data)
print(data)
output:(91, 34, 49, 34, 44, 34, 50, 34, 44, 34, 51, 34, 93)
但我需要如下输出。 预期输出:
data = (1,2,3)
一种方法是先解码字节串,然后使用ast.literal_eval
将其转换为列表:
from ast import literal_eval
data = b'["1","2","3"]'
res = literal_eval(data.decode("utf-8"))
res = tuple(res)
print(res)
输出
('1', '2', '3')
我们不知道 encoded
原始字节串是怎样的。
假设他们在 json:
import json
tuple(json.loads(data))
#('1', '2', '3')
如果它们(不幸的是)是 py 表示:
tuple(eval(data))
#('1', '2', '3')
主要问题是,它们是如何编码成字符串的?