Home > front end >  Can bytes-shape string parse to bytes in Python
Can bytes-shape string parse to bytes in Python

Time:08-05

I want to convert string like this to bytes what can encode 'utf-8':

S = "b'\\xec\\xa7\\x80\\xeb\\xb0\\x98\\xec\\x84\\xa0"

Its type is obviously string as you see:

print(type(S)) #<class 'str'>

This bytes-shape string is actually Korean (when it is parsed to bytes) like this:

B = b'\xec\xa7\x80\xeb\xb0\x98\xec\x84\xa0'
print(str(B,'utf-8')) #result is "지반선"

How can I convert the string type like S to bytes type like B?

CodePudding user response:

You can use eval builtin function to achieve this but be careful, you should never use eval in production code as it's exploitable and unsafe.

Here is an example on how you could achieve this using eval:

>>> S = "b'\\xec\\xa7\\x80\\xeb\\xb0\\x98\\xec\\x84\\xa0'"
>>> eval(S)
b'\xec\xa7\x80\xeb\xb0\x98\xec\x84\xa0'

you can use ast.literal_eval as a safe alternative of eval

Be careful though: with ast.literal_eval you cannot use variables and do mathematical operations as it's not parsed as a non-safe string;

From the docs:

This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

>>> import ast
>>> S = "b'\\xec\\xa7\\x80\\xeb\\xb0\\x98\\xec\\x84\\xa0'"
>>> ast.literal_eval(S)
b'\xec\xa7\x80\xeb\xb0\x98\xec\x84\xa0'

I noticed that in your example, you didn't have the closing ' so to add it here is a simple demonstration on how to do this:

>>> S = "b'\\xec\\xa7\\x80\\xeb\\xb0\\x98\\xec\\x84\\xa0"
>>> if not S.endswith("'"):
...    S = f"{S}'"
>>> S
"b'\\xec\\xa7\\x80\\xeb\\xb0\\x98\\xec\\x84\\xa0'"

CodePudding user response:

You can use eval

S = "b'\\xec\\xa7\\x80\\xeb\\xb0\\x98\\xec\\x84\\xa0'"
# in case S has no closing ' you can just append it S  = "'"
S = eval(S)
print(type(S))

However be careful, eval evaluates any code so running it with external string can make a lot harm.

  • Related