如何将字符串字节码转换为python中的字节码?

How to convert string bytecode to bytecode in python?

我有一个像这样的字符串字节码"b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='"

我希望它是像这样的字节码类型 b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='

ast 模块可用于将字节码 base64 str 转换为适当的字节串 (bytes) 对象。

示例:

import ast

x = "b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='"
out = ast.literal_eval(x)

输出:

>>> out
b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='

>>> type(out)
bytes