Search code examples
pythonutf-8decode

How Python decodes UTF8 Encoding in String Format


Now there is a string of utf-8:

s = '\\346\\235\\216\\346\\265\\267\\347\\216\\211'

I need to decode it, but now I only do it in this way:

result = eval(bytes(f"b'{s}'", encoding="utf8")).decode('utf-8')

This is not safe, so is there a better way?


Solution

  • Use ast.literal_eval(), it's not unsafe.

    Then you don't need to call bytes(), since it will return a byte string.

    result = ast.literal_eval(f"b'{s}'").decode('utf-8')