1. 程式人生 > >寫檔案以及讀取檔案並賦值給一個數組

寫檔案以及讀取檔案並賦值給一個數組

A-已知資料數量

寫檔案,已知要寫3個數據

#get the value and store in a variable  
MarkNum=self.line_edit1.text()  
LiveMarkNum=self.line_edit2.text()  
ClipsaAdd2PL=self.line_edit3.text()  
#write the variable to a txt file  
filename=('C:\\FL_Test\\Result01.txt')  
with open(filename,'w') as file_object:  
    #clean the file?  
    file_object.truncate()  
    #wirte the value  
    file_object.write(MarkNum+'\n')  
    file_object.write(LiveMarkNum+'\n')  
    file_object.write(ClipsaAdd2PL+'\n')  
    #close the file  
    file_object.close() 

讀取檔案並賦值給陣列:

res01Val=['0','0','0']  
res01="C:\\FL_Test\\Result01.txt"  
res01Val[0]=str(linecache.getline(res01,1))  
res01Val[1]=str(linecache.getline(res01,2))  
res01Val[2]=str(linecache.getline(res01,3))

B-不知道資料數量,讀取並賦值

後面更新