你需要逃避反斜杠.
>>> eval('"hello \n"') Traceback (most recent call last): File "", line 1, in File " ", line 1 "hello ^ SyntaxError: EOL while scanning string literal >>> eval('"hello \\n"') 'hello \n' >>> print(eval('"hello \\n"')) hello >>>
没有那个转义,Python会看到这个代码(这是一个明显的错误):
"hello "
而不是所需的代码:
"hello \n"