简体   繁体   English

命令行中的if语句和一行python脚本

[英]If statements and one line python scripts from command line

Why do I receive a syntax error for the following one liner python code? 为什么我收到以下一个内衬python代码的语法错误?

python -c 'import re; if True: print "HELLO";'
  File "<string>", line 1
    import re; if True: print "HELLO";
                ^
SyntaxError: invalid syntax

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? 如何从命令行更改一行以在一行上执行我想要的脚本?

One option to work around this limitation is to specify the command with the $'string' format using the newline escape sequence \\n . 解决此限制的一种方法是使用换行符\\n指定$'string'格式的命令。

python -c $'import re\nif True: print "HELLO";'

Note: this is supported by shells such as bash and zsh, but is not valid POSIX sh. 注意:bash和zsh之类的shell支持此功能,但它不是有效的POSIX sh。

As mentioned by @slaadvak, there are some other workarounds here: Executing Python multi-line statements in the one-line command-line 如@slaadvak所述,这里还有其他解决方法: 在单行命令行中执行Python多行语句

The problem isn't with the import statement specifically, its that you have anything before a control flow statement. 问题不是专门与import语句有关的,它是控制流语句之前的所有内容。 This won't work, either: 这将不起作用,或者:

dan@dan:~> python -c 'a = "1234" ; if True: print "hi"'
  File "<string>", line 1
    a = "1234" ; if True: print "hi"
                  ^
SyntaxError: invalid syntax

According to the Python reference ( https://docs.python.org/2/reference/compound_stmts.html ), ';' 根据Python参考( https://docs.python.org/2/reference/compound_stmts.html ),“;” can only be used to combine "simple statements" together. 只能用于将“简单语句”组合在一起。 In this case you're combining the simple statement import re , with if True: . 在这种情况下,您将简单的语句import reif True:结合在一起。 if True isn't a simple statement, because it's introducing flow control, and is therefore a compound statement. if True不是简单的语句,因为它引入了流控制,因此是复合语句。 At least that's how I interpret the documentation. 至少这就是我解释文档的方式。

Here's the full text from the Python reference: 这是Python参考资料的全文:

Compound statements consist of one or more 'clauses.' 复合语句由一个或多个“子句”组成。 A clause consists of a header and a 'suite.' 子句由标题和“套件”组成。 The clause headers of a particular compound statement are all at the same indentation level. 特定复合语句的子句标题都位于相同的缩进级别。 Each clause header begins with a uniquely identifying keyword and ends with a colon. 每个子句头均以唯一标识的关键字开头,以冒号结尾。 A suite is a group of statements controlled by a clause. 套件是由子句控制的一组语句。 A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on subsequent lines 套件可以是在标头的冒号之后,与标头在同一行上的一个或多个用分号分隔的简单语句,也可以是后续行上的一个或多个缩进语句

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | funcdef
                   | classdef
                   | decorated
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

You can embed newlines directly in the argument. 您可以直接在参数中嵌入换行符。

$ python -c 'import re
> if True:
>  print "HELLO"
> '

Why do I receive a syntax error for the following one liner python code? 为什么我收到以下一个内衬python代码的语法错误?

Python grammar might forbid small_stmt ';' compound_stmt Python语法可能禁止small_stmt ';' compound_stmt small_stmt ';' compound_stmt . small_stmt ';' compound_stmt -c argument is probably is interpreted as file_input : -c参数可能被解释为file_input

fileinput: (NEWLINE | stmt)* ENDMARKER
stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: import_stmt <in this case>
compound_stmt: if_stmt <in this case>

Note: there is a newline at the end of simple_stmt . 注意: simple_stmt的末尾有一个换行符。 if_stmt is not small_stmt it can't follow another small_stmt after ';' if_stmt不是small_stmt它不能跟在';'之后';'另一个small_stmt . A newline is necessary to introduce compound_stmt after small_stmt . small_stmt之后需要换行符来引入compound_stmt

It is not an issue because bash allows multiline arguments, just don't close the opening single quote until you done eg: 这不是问题,因为bash允许多行参数,只是在完成以下操作之前不要关闭开头的单引号:

$ python -c '
> import re
> if 1:
>   print(1)
> '
1

Note: > are printed by the shell itself here. 注意: >由外壳本身在此处打印。 It is not entered by hand. 它不是手动输入的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM