2015-04-03 52 views

回答

6

您可以使用pickle module將任意Python實例串行化爲一個文件,並在稍後將它們恢復到原始狀態。

這包括Counter對象:

>>> import pickle 
>>> from collections import Counter 
>>> counts = Counter('the quick brown fox jumps over the lazy dog') 
>>> with open('/tmp/demo.pickle', 'wb') as outputfile: 
...  pickle.dump(counts, outputfile) 
... 
>>> del counts 
>>> with open('/tmp/demo.pickle', 'rb') as inputfile: 
...  print pickle.load(counts) 
... 
>>> with open('/tmp/demo.pickle', 'rb') as inputfile: 
...  print pickle.load(inputfile) 
... 
Counter({' ': 8, 'o': 4, 'e': 3, 'h': 2, 'r': 2, 'u': 2, 't': 2, 'a': 1, 'c': 1, 'b': 1, 'd': 1, 'g': 1, 'f': 1, 'i': 1, 'k': 1, 'j': 1, 'm': 1, 'l': 1, 'n': 1, 'q': 1, 'p': 1, 's': 1, 'w': 1, 'v': 1, 'y': 1, 'x': 1, 'z': 1}) 
+0

非常感謝... – 2015-04-03 16:47:39

相關問題