Dev*_*air -2 python csv parsing for-loop python-2.7
如何让我的其他打印只打印一次而不是每行不存在该字符串?我尝试通过回击几层来移动它,但它不起作用.我理解逻辑,但我不知道如何限制它.我正在为我的解析脚本添加一点点练习,随时学习,但是这个让我感到高兴.谢谢!
import csv
# Testing finding something specifical in a CSV, with and else
testpath = 'C:\Users\Devin\Downloads\users.csv'
developer = "devin"
with open (testpath, 'r') as testf:
testr = csv.reader(testf)
for row in testr:
for field in row:
if developer in row:
print row
else:
print developer + " does not exist!"
Run Code Online (Sandbox Code Playgroud)
在Python中,您可以else在for循环中附加一个子句.例如
>>> for i in range(10):
... if i == 5: break # this causes the else statement to be skipped
... else:
... print 'not found'
...
Run Code Online (Sandbox Code Playgroud)
5发现了注释,因此不执行else语句
>>> for i in range(10):
... if i == 15: break
... else:
... print 'not found'
...
not found
Run Code Online (Sandbox Code Playgroud)
在第一个套件中执行的break语句终止循环而不执行else子句的套件.在第一个套件中执行的continue语句会跳过套件的其余部分并继续下一个项目,如果没有下一个项目,则使用else子句.