I have a string bytecode like this "b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='"
and I want it as bytecode type like this b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='
CodePudding user response:
There is no need for string manipulation here. The ast
module can be used to convert the bytecode base64 str
into a proper bytestring (bytes
) object.
Example:
import ast
x = "b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='"
out = ast.literal_eval(x)
Output:
>>> out
b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='
>>> type(out)
bytes
CodePudding user response:
You can use strip
data = '"b\'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w==\'"'
data = data.strip('\"')
print(data)
Output :
b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='