Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the invert operation of bytes repr() in python3?

a = b'\x00\x01'
ra = repr(a)   # ra == "b'\\x00\\x01'"
assert invert_repr(ra) == a

What is the correct form of invert_repr? string_escape & unicode_escape?

like image 293
ShenLei Avatar asked Oct 19 '25 16:10

ShenLei


1 Answers

Use eval or equivalent:

from ast import literal_eval
a = b'\x00\x01'
ra = repr(a)
assert literal_eval(ra) == eval(ra) == a # no error

ast.literal_eval is safer than eval.

like image 161
vaultah Avatar answered Oct 21 '25 04:10

vaultah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!