星期四, 11月 05, 2009

Python 輸出資料結構

為 spider 做準備,蒐集一些用的到module!!

python 有個標準函式庫(cPickle | pickle)可以讓使用者直接輸出 dict,list,...即使是更複雜的資列結構都沒問題。因此拿來備份,暫存,還原都很方便。

[Swaroop, C. H. 簡明 Python 教程 第12章 輸入/輸出]

把程式碼備份一下

#!/usr/bin/python
# Filename: pickling.py

import cPickle as p
#import pickle as p

shoplistfile = 'shoplist.data'
# the name of the file where we will store the object

#shoplist = ['apple', 'mango', 'carrot']
shoplist = {"aaa":"1","bbb":"2"}

# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()

del shoplist # remove the shoplist

# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist

沒有留言: