2015-08-27 26 views

回答

1

迭代方法是基於Iterating over dictionaries using 'for' loops

c = 0 
f = open('%d.txt'%(c),'w') 
for key, value in d.iteritems(): 
    print>>f,"%s:%s"%(str(key),str(value)) 
    c += 1 
    if c % 100 == 0: 
     f.close() 
     f = open('%d.txt'%(c/100),'w') 
f.close() 
+0

請注意,這適用於Python2。如果在Python3中,您需要使用'd.items()'遍歷字典的鍵值對(除了使用'print'函數的括號外)。 – ray

相關問題