我有一长串代码,我希望在多行之间分解.我使用什么,语法是什么?
例如,添加一串字符串,
e = 'a' + 'b' + 'c' + 'd'
Run Code Online (Sandbox Code Playgroud)
并将它分成两行:
e = 'a' + 'b' +
'c' + 'd'
Run Code Online (Sandbox Code Playgroud) 如下面的代码所示,该groupby行应该被注释掉:
lines = fileinput.input(fin) \
| take(300) \
| where(lambda x: not x.strip().endswith(',,,,,')) \
\ # | groupby(lambda x: x[42])
| teeFile(fout,100)
Run Code Online (Sandbox Code Playgroud)
但是上面的语法 - 以及它的几个变体 - 不起作用:
\ # | groupby(lambda x: x[42])
^
SyntaxError: unexpected character after line continuation character
Run Code Online (Sandbox Code Playgroud)
已尝试的另一种变化:
# | groupby(lambda x: x[42]) \
Run Code Online (Sandbox Code Playgroud)
有没有办法评论一个需要延续字符的较长陈述的一部分?或者我们只是运气不好 - 就像python无法(/不愿意)支持内联评论一样?
我在 2.7
更新 此处是对代码段的一个小更新,使其完全自包含.
import sys, pipe, fileinput ; from pipe import *;
lines = fileinput.input(fin) \
| take(300) \
| where(lambda x: not x.strip().endswith(',,,,,,,,')) \
# …Run Code Online (Sandbox Code Playgroud) 当我编写以下pyspark命令时:
# comment 1
df = df.withColumn('explosion', explode(col('col1'))).filter(col('explosion')['sub_col1'] == 'some_string') \
# comment 2
.withColumn('sub_col2', from_unixtime(col('explosion')['sub_col2'])) \
# comment 3
.withColumn('sub_col3', from_unixtime(col('explosion')['sub_col3']))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
.withColumn('sub_col2', from_unixtime(col('explosion')['sub_col2']))
^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)
有没有办法在pyspark的多行命令行之间写注释?