【发布时间】:2012-06-11 15:44:48
【问题描述】:
我认为 elif: 是
的简写else:
if:
但无法使用
for-elif:
只有
对于-否则:如果:
在这段代码中:
for line in source:
change_next = False
for dataset,artnr,revision in datasets:
if dataset in line:
change_next = True
print " ** " + dataset + " found"
datasets.remove((dataset,artnr,revision))
break
else:
if line.startswith("DstID:"):
print line.replace("DstID:","").rstrip()
if change_next and "Partno:" in line:
destination.write("Partno: " + artnr + "\n")
print "Partno: " + artnr
elif change_next and "Revno:" in line:
destination.write("Revno:" + revision + "\n")
print "Revno:" + revision
else:
destination.write(line)
感谢您到目前为止的回复,我现在的问题是:这是这样做的方式吗?如果一行中没有任何(已知的)数据集,那么如果它是数据集,我想打印它?
【问题讨论】:
-
我已经编写 Python 大约 7 年了,但我从来不知道
for语句中的else子句... -
@AndrewJaffe:这是可以理解的。它不是特别有用。我想我用过它的次数屈指可数。
-
@JoelCornett 我一直在使用它。
标签: python for-loop python-2.7 if-statement