【发布时间】:2020-01-12 22:32:39
【问题描述】:
for desc in database.desc:
if sub in desc:
station = desc.split(";")
station.pop(0)
station.pop(0)
station.pop(1)
if len(station) > 1:
station.pop()
print(station)
else:
""
print(station)
第一个打印语句给出了我想要的所有值。当我在 for 循环之外打印语句时。它只给了我 csv 文件的最后一个值。
【问题讨论】:
-
station在循环的每次迭代中都会被覆盖,因此一旦循环结束,它的值将是它设置的最后一个值,这将是循环中分配的最后一个值。如果要保留所有值,请使用列表并附加到它。