如下面的代码所示,该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(',,,,,,,,')) \
# | groupby(lambda x: x[42]) \
| tee
Run Code Online (Sandbox Code Playgroud)
它现在只包括一个导入.我在ipythonvs中遇到了不同的错误intellij:
ipython :
File "<ipython-input-2-60c5dbee382d>", line 3
| tee
^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)
intellij :
File "<ipython-input-30-1f7b64578a1f>", line 16
lines = fileinput.input(fin) | take(300) | where(lambda x: not x.strip().endswith(',,,,,,,,,,,,,,,,,,,,,')) \ # | groupby(lambda x: x[42])
^
SyntaxError: unexpected character after line continuation character
Run Code Online (Sandbox Code Playgroud)
对括号使用隐式行继续:
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)
在未闭合的括号(,括号[或大括号内{,Python将自动执行行继续,甚至跨越带注释的行.使用反斜杠进行行连接的规则不允许在带注释的行上继续行.
| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |