Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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代码_Python - Fatal编程技术网

编写将正确输入换行命令的python代码

编写将正确输入换行命令的python代码,python,Python,我正在尝试编写一个python代码(我们称之为script_a),它本身将编写另一个python脚本(script_B),当执行该脚本时,将创建一个文本文件,其中包含几行python代码,每行都应以换行命令结束。问题是,在脚本_A中的代码中输入换行命令会导致问题 这是我的密码: 脚本_A中的行: main=open('Script_B', "wb") main.write("fo=open('textfile', 'a')\n") main.write("fo.write('text to be

我正在尝试编写一个python代码(我们称之为script_a),它本身将编写另一个python脚本(script_B),当执行该脚本时,将创建一个文本文件,其中包含几行python代码,每行都应以换行命令结束。问题是,在脚本_A中的代码中输入换行命令会导致问题

这是我的密码:

脚本_A中的行:

main=open('Script_B', "wb")
main.write("fo=open('textfile', 'a')\n")
main.write("fo.write('text to be ended with a newline command\n')\n")
main.close()
然后,执行脚本_A将创建包含以下文本的脚本_B:

fo=open('textfile', 'a')
fo.write(‘text to be ended with a newline command
‘)
脚本_B将不会运行:

File "Script_B", line 2
fo.write('text to be ended with a newline command
                                                ^
SyntaxError: EOL while scanning string literal
应该有用。。。(注意\n在命令内转义)

(请注意,这是使代码正常工作的多种方法之一)

应该有用。。。(注意\n在命令内转义)

(请注意,这是使代码正常工作的多种方法之一)

应该有用。。。(注意\n在命令内转义)

(请注意,这是使代码正常工作的多种方法之一)

应该有用。。。(注意\n在命令内转义)


(请注意,这是使代码正常工作的多种方法之一)

问题是您需要避开反斜杠

main.write("fo.write('text to be ended with a newline command\n')\n")
我会给你

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
相反,你应该使用

main.write("fo.write('text to be ended with a newline command\\n')\n")
(注意额外的反斜杠)以获取

我建议您使用原始字符串,以避免此类问题。原始字符串前面有一个
r
字符,它将任何反斜杠解释为实际的反斜杠,而不是转义字符

作为原始字符串:

main.write(r"fo.write('text to be ended with a newline command\n')\n")
(注意字符串前面的
r
)将为您提供

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
唯一的问题是,您现在在末尾有一个额外的
\n
,您可以通过编写

main.write(r"fo.write('text to be ended with a newline command\n')" + "\n")

这正好提供了您想要的内容,并且是一种很好的分离代码的方法——代码应该是原始字符串,以避免反斜杠的问题——以及每个字符串末尾的换行符。

问题是您需要转义反斜杠

main.write("fo.write('text to be ended with a newline command\n')\n")
我会给你

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
相反,你应该使用

main.write("fo.write('text to be ended with a newline command\\n')\n")
(注意额外的反斜杠)以获取

我建议您使用原始字符串,以避免此类问题。原始字符串前面有一个
r
字符,它将任何反斜杠解释为实际的反斜杠,而不是转义字符

作为原始字符串:

main.write(r"fo.write('text to be ended with a newline command\n')\n")
(注意字符串前面的
r
)将为您提供

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
唯一的问题是,您现在在末尾有一个额外的
\n
,您可以通过编写

main.write(r"fo.write('text to be ended with a newline command\n')" + "\n")

这正好提供了您想要的内容,并且是一种很好的分离代码的方法——代码应该是原始字符串,以避免反斜杠的问题——以及每个字符串末尾的换行符。

问题是您需要转义反斜杠

main.write("fo.write('text to be ended with a newline command\n')\n")
我会给你

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
相反,你应该使用

main.write("fo.write('text to be ended with a newline command\\n')\n")
(注意额外的反斜杠)以获取

我建议您使用原始字符串,以避免此类问题。原始字符串前面有一个
r
字符,它将任何反斜杠解释为实际的反斜杠,而不是转义字符

作为原始字符串:

main.write(r"fo.write('text to be ended with a newline command\n')\n")
(注意字符串前面的
r
)将为您提供

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
唯一的问题是,您现在在末尾有一个额外的
\n
,您可以通过编写

main.write(r"fo.write('text to be ended with a newline command\n')" + "\n")

这正好提供了您想要的内容,并且是一种很好的分离代码的方法——代码应该是原始字符串,以避免反斜杠的问题——以及每个字符串末尾的换行符。

问题是您需要转义反斜杠

main.write("fo.write('text to be ended with a newline command\n')\n")
我会给你

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
相反,你应该使用

main.write("fo.write('text to be ended with a newline command\\n')\n")
(注意额外的反斜杠)以获取

我建议您使用原始字符串,以避免此类问题。原始字符串前面有一个
r
字符,它将任何反斜杠解释为实际的反斜杠,而不是转义字符

作为原始字符串:

main.write(r"fo.write('text to be ended with a newline command\n')\n")
(注意字符串前面的
r
)将为您提供

fo.write('text to be ended with a newline command
')
fo.write('text to be ended with a newline command\n')\n
唯一的问题是,您现在在末尾有一个额外的
\n
,您可以通过编写

main.write(r"fo.write('text to be ended with a newline command\n')" + "\n")

这正好提供了您想要的内容,并且是一种很好的方法,可以将代码(应该在原始字符串中以避免反斜杠问题)和每个字符串末尾的换行符分隔开。

单引号中的换行符需要转义序列字符单引号中的换行符需要转义序列字符单引号中的换行符将需要转义序列字符单引号中的换行符将需要转义序列字符