I have string like this that has a stringified byte substring inside of it as follows:
some_string = "b'Hurricane Mitch\\n'"
What is the best way to extract the nested b' string so that I can decode it properly in utf8?
I have string like this that has a stringified byte substring inside of it as follows:
some_string = "b'Hurricane Mitch\\n'"
What is the best way to extract the nested b' string so that I can decode it properly in utf8?
The most straightforward approach (that's still more powerful than you need, but probably not a security risk) is ast.literal_eval.
from ast import literal_eval
some_string = "b'Hurricane Mitch\\n'"
literal_eval(some_string).decode('utf-8') # 'Hurricane Mitch\n'