一种方法是使用
else:
块以捕获内部循环未中断的情况(在这种情况下,您希望
continue
),然后是
break
如果
else
还没有继续:
coords = []
with open('ADM2_hits.csv') as csvFile:
csvReader = csv.reader(csvFile)
next(csvReader)
for line in csvReader:
keyword = line[1]
for dic in adm2_geonames:
for x in dic["geonames"]:
if(keyword == x['name']):
line.append(x['lat'])
line.append(x['lng'])
coords.append(line)
break
else:
continue
break
print(coords)
请注意,只需直接转到
dic["geonames"]
而不是在整本字典上循环;字典的全部意义在于,你可以通过它的键直接跳转到一个给定的条目,而不必反复搜索整个条目。