给定(伪代码):
for city in cities:
for info in city:
#add some stuff to a list
#add each list to a list (creating a list of lists)
with open('file.csv', 'ab') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(the list of lists) #in order of list
所以我打开每个城市的文件并附上每个城市的每个列表 列表的名单。什么是实现这一目标的更有效(更快)的方式?
答案 0 :(得分:1)
我觉得我错过了这一点,但它不仅仅是一个简单的重新排列,只需打开一次文件然后在每个城市循环结束时写下来吗?
with open('file.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
for city in cities:
for info in city:
#add some stuff to a list
#add each list to a list (creating a list of lists)
writer.writerows(the list of lists)
答案 1 :(得分:0)
如果你不需要“人”阅读文件,你可以使用pickle模块,只需将2d列表转储到文件。然后,您的程序可以在以后读回数据,并且数据结构将是完整的。