Converting python string into bytes directly without eval()

前端 未结 1 468
一个人的身影
一个人的身影 2020-12-07 04:00

eval() seems to be dangerous to use when processing unknown strings, which is what a part of my project is doing.

For my project I have a string, called:

<         


        
相关标签:
1条回答
  • 2020-12-07 04:57

    yes, with ast.literal_eval which is safe since it only evaluates literals.

    >>> import ast
    >>> stringAsByte = "b'a'"
    >>> ast.literal_eval(stringAsByte)
    b'a'
    
    0 讨论(0)
提交回复
热议问题