Reputation: 23
str1 = "b'E\\x00\\x05\\xad\\\\\\xd9\\x00\\x00v\\x11\\xfeA_\\xc2&j\\xb6\\xfa\\xa6\\xfe\\xd9?F\\xc9\\x05\\x99n\\x8a'"
b1 = b'E\\x00\\x05\\xad\\\\\\xd9\\x00\\x00v\\x11\\xfeA_\\xc2&j\\xb6\\xfa\\xa6\\xfe\\xd9?F\\xc9\\x05\\x99n\\x8a'
how to transfer str1 which is <class 'str'> to b1 which is <class 'bytes'>?
Upvotes: 1
Views: 73
Reputation: 2537
You can use ast.literal_eval
, to evaluate any expression in a string
>>> import ast
>>> str1 = "b'E\\x00\\x05\\xad\\\\\\xd9\\x00\\x00v\\x11\\xfeA_\\xc2&j\\xb6\\xfa\\xa6\\xfe\\xd9?F\\xc9\\x05\\x99n\\x8a'"
>>> ast.literal_eval(str1)
b'E\x00\x05\xad\\\xd9\x00\x00v\x11\xfeA_\xc2&j\xb6\xfa\xa6\xfe\xd9?F\xc9\x05\x99n\x8a'
>>>
Upvotes: 1