在多行语句之间插入注释(带有行继续) [英] Put comments in between multi-line statement (with line continuation)

查看:110
本文介绍了在多行语句之间插入注释(带有行继续)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写以下pyspark命令时:

When i write a following pyspark command:

# 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']))

我收到以下错误:

.withColumn('sub_col2', from_unixtime(col('explosion')['sub_col2']))
^
IndentationError: unexpected indent

是否可以在pyspark的多行命令行之间编写注释?

Is there a way to write comments in between the lines of multiple-line commands in pyspark?

推荐答案

这不是 pyspark 的问题,而是违反python语法的。

This is not a pyspark issue, but rather a violation of python syntax.

请考虑以下示例:

a, b, c = range(3)
a +\
# add b
b +\
# add c
c

结果是:

    a +# add b
              ^
SyntaxError: invalid syntax

\ 是连续字符,python将下一行中的所有内容解释为紧随其后发生的错误,

The \ is a continuation character and python interprets anything on the next line as occurring immediately after, causing your error.

一种解决方法是改用括号:

One way around this is to use parentheses instead:

(a +
# add b
b +
# add c
c)

分配给变量时,这看起来像是

When assigning to a variable this would look like

# do a sum of 3 numbers
addition = (a +
            # add b
            b +
            # add c
            c)

或者在您的情况下:

# 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'])))

这篇关于在多行语句之间插入注释(带有行继续)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆