Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用另一个文字替换文字\n\\n_Python - Fatal编程技术网

Python 用另一个文字替换文字\n\\n

Python 用另一个文字替换文字\n\\n,python,Python,我正在努力将“\n”替换为文字,而不是将换行符替换为“\n” 示例:文本是mytext='hello\n',我需要这样的输出 mytext='hello\\n'我无法通信,但如果我理解你的意思,你能做什么 mytext.replace('\n', '\\n') 你试过使用这种方法吗?你能详细说明你的问题吗?非常感谢。事实上,通过replace('\\n','\\\\n')

我正在努力将“\n”替换为文字,而不是将换行符替换为“\n” 示例:文本是
mytext='hello\n'
,我需要这样的输出
mytext='hello\\n'

我无法通信,但如果我理解你的意思,你能做什么

mytext.replace('\n', '\\n')

你试过使用这种方法吗?你能详细说明你的问题吗?非常感谢。事实上,通过
replace('\\n','\\\\n')