2014-03-05 90 views
0

我有以下代碼,將列表寫入文件並以列表格式讀取它的最佳方式是什麼?任何幫助將不勝感激。將文件和閱讀列表寫入文件

l=[ 
      "[ 'ARAJunkyard_Build_Branch' , 7 , 6 , 1 ]", 
      "[ 'ARAJunkyard_Build_Head' , 10 , 10 , 0 ]", 
      "[ 'ARAJunkyard_DevUnitTests_Branch' , 10 , 9 , 1 ]", 
      "[ 'ARAJunkyard_DevUnitTests_HEAD' , 10 , 0 , 10 ]", 
      "[ 'ARAJunkyard_Functional_Tests' , 7 , 2 , 5 ]"] 

回答

1

只需使用pickle模塊,您可以使用pickle存儲幾乎任何對象:

import pickle 

l=[ 
      "[ 'ARAJunkyard_Build_Branch' , 7 , 6 , 1 ]", 
      "[ 'ARAJunkyard_Build_Head' , 10 , 10 , 0 ]", 
      "[ 'ARAJunkyard_DevUnitTests_Branch' , 10 , 9 , 1 ]", 
      "[ 'ARAJunkyard_DevUnitTests_HEAD' , 10 , 0 , 10 ]", 
      "[ 'ARAJunkyard_Functional_Tests' , 7 , 2 , 5 ]"] 

with open("test.dat", "wb") as fp: 
    pickle.dump(l, fp) 


with open("test.dat", "rb") as fp: 
    l2 = pickle.load(fp) 

print l2 

http://docs.python.org/2/library/pickle.html

+0

Thanks..buddy..it真的幫助我.. –