如果命令行语句和一行Python脚本 [英] If statements and one line python scripts from command line
问题描述
为什么我收到一个语法错误以下单行蟒蛇code?
Why do I receive a syntax error for the following one liner python code?
python -c 'import re; if True: print "HELLO";'
File "<string>", line 1
import re; if True: print "HELLO";
^
SyntaxError: invalid syntax
以下code的作品就好。
The following code works just fine
python -c 'if True: print "HELLO";'
我
如何修改我的一行执行的命令行单行我打算脚本?
How can I change my one line to execute my intended script on a single line from the command line?
推荐答案
要解决此限制的一种方法是使用指定的 $字符串
格式命令换行转义序列 \\ n
。
One option to work around this limitation is to specify the command with the $'string'
format using the newline escape sequence \n
.
python -c $'import re\nif True: print "HELLO";'
请注意:这是被炮弹如bash和zsh的支持,但不是有效的POSIX SH
Note: this is supported by shells such as bash and zsh, but is not valid POSIX sh.
正如@slaadvak提到的,这里还有一些其他的解决方法:<一href=\"http://stackoverflow.com/questions/2043453/executing-python-multi-line-statements-in-the-one-line-command-line\">Executing在单行命令行 Python的多行语句
As mentioned by @slaadvak, there are some other workarounds here: Executing Python multi-line statements in the one-line command-line
这篇关于如果命令行语句和一行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!