Process escape sequences in a string in Python

前端 未结 6 1331
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 03:56

Sometimes when I get input from a file or the user, I get a string with escape sequences in it. I would like to process the escape sequences in the same way that Python proc

6条回答
  •  迷失自我
    2020-11-22 04:50

    This is a bad way of doing it, but it worked for me when trying to interpret escaped octals passed in a string argument.

    input_string = eval('b"' + sys.argv[1] + '"')
    

    It's worth mentioning that there is a difference between eval and ast.literal_eval (eval being way more unsafe). See Using python's eval() vs. ast.literal_eval()?

提交回复
热议问题