Python 如何处理for循环的这个小问题
当我处理下面的代码时,python返回Python 如何处理for循环的这个小问题,python,for-loop,Python,For Loop,当我处理下面的代码时,python返回 for(temp in range(len(history_list))): ^ SyntaxError: invalid syntax" 我真的无法找出错误的原因,如果您有任何想法,请留下评论,谢谢 with open('history.csv', 'r',newline='') as file: history_list = csv.reader(cs
for(temp in range(len(history_list))):
^
SyntaxError: invalid syntax"
我真的无法找出错误的原因,如果您有任何想法,请留下评论,谢谢
with open('history.csv', 'r',newline='') as file:
history_list = csv.reader(csvfile)
for(temp in range(len(history_list))):
if(keyword==history_list[temp][0]):
#do something
break
不需要(
之后的用于)
重写为
for temp in range(len(history_list)):
blah blah
在更具python风格的注释中,重新考虑将循环编写为:
for item in history_list:
blah blah
这不是C++,XD,您不需要用于<代码>的括号,for循环< /代码>请重复,并且从“教我这个基本语言特征”中讨论堆栈溢出问题。堆栈溢出并不用于替换现有教程和文档。1)For循环应该在with块中,2)history_列表将是一个迭代器,因此当您在For循环中尝试时,您不能索引到,即history_列表[temp]不起作用。添加了一个建议,使代码更具Python风格。@930727fre-理想情况下,您应该在发布问题之前对代码进行校对,但是你也可以删除这样的问题,我建议你在这里使用PyFlakes这样的工具来检查你的代码。它集成到大多数代码编辑器中,使其易于使用。