python中數據的保存


1.將list中的數據寫入到excel文件中

利用python包numpy(實現方式應該有許多種,這里只是記錄成功實現的一種)中的savetxt

局限性:要保存的list可以為[1,2,3,4,5]這種形式,也可以是[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]這種形式,但如果是第二種情況(list中每個元素都為list)時,list中的每個list所含有元素個數要相同,這是因為該方法需要先將python的list數據轉化為array類型

import numpy as np
# python list that is needed to be save in the csv file
data_list = [1,2,3,4,5,6,7,8]
print type(data_list)
# convert list to array
data_array = np.array(data_list)
print type(data_array)
# saving...
np.savetxt('data.csv',data_array,delimiter=',')
print 'Finish saving csv file'

結果:

<type 'list'>
<type 'numpy.ndarray'>
Finish saving csv file

 1 import numpy as np
 2 # python list that is needed to be save in the csv file
 3 data_list = [[1,2,3,4,5,6,7,8],[9,10,11,12,14,1,2,3],[4,3,2,1,3,6,7,8]]
 4 print type(data_list)
 5 # convert list to array
 6 data_array = np.array(data_list)
 7 print type(data_array)
 8 # saving...
 9 np.savetxt('data.csv',data_array,delimiter=',')
10 print 'Finish saving csv file'

結果

<type 'list'>
<type 'numpy.ndarray'>
Finish saving csv file

 
        

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM