0

I'm personally looking for a way to convert a string which includes bytecode into a bytecode type but I'm also curious if it is possible to do this process for essentially any variable type without using eval.

Working example using eval:

a = "b'gAAAAABhI1vlZaaVcD7VcXpk5kEpqtn5ItujPJUnP92QgwUOc07ulG95YbCyn9M0zolk-z04GywKmGehChhrj27KA5BQVafQ8w=='"
print(type(eval(a)))

Output:

<class 'bytes'>
2
  • "any type"? That means reinventing eval(), with all its consequences. Commented Aug 23, 2021 at 8:49
  • Wow, I somehow didn't see that the same question was asked. Either way, ast.literal_eval is exactly what I was looking for. Thanks! Commented Aug 26, 2021 at 4:55

1 Answer 1

0

You could solve it with str.encode:

>>> a = "b'gAAAAABhI1vlZaaVcD7VcXpk5kEpqtn5ItujPJUnP92QgwUOc07ulG95YbCyn9M0zolk-z04GywKmGehChhrj27KA5BQVafQ8w=='"
>>> a[3:-2].encode()
b'AAAAABhI1vlZaaVcD7VcXpk5kEpqtn5ItujPJUnP92QgwUOc07ulG95YbCyn9M0zolk-z04GywKmGehChhrj27KA5BQVafQ8w='
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.