python學(xué)習(xí)將數(shù)據(jù)寫入文件并保存方法
python將文件寫入文件并保存的方法:
使用python內(nèi)置的open()函數(shù)將文件打開,用write()函數(shù)將數(shù)據(jù)寫入文件,最后使用close()函數(shù)關(guān)閉并保存文件,這樣就可以將數(shù)據(jù)寫入文件并保存了。
示例代碼如下:
file = open('ax.txt', ’w’)file.write(’hskhfkdsnfdcbdkjs’)file.close()
執(zhí)行結(jié)果:
內(nèi)容擴(kuò)展:
python將字典中的數(shù)據(jù)保存到文件中
d = {’a’:’aaa’,’b’:’bbb’}s = str(d)f = open(’dict.txt’,’w’)f.writelines(s)f.close()
實(shí)例2:
def main():list1 = [[’西瓜’,’蘭州’,’first’,20],[’香蕉’,’西安’,’second’,30],[’蘋果’,’銀川’,’third’,40],[’桔子’,’四川’,’fourth’,40]]output = open(’data.xls’,’w’,encoding=’gbk’)output.write(’fruitt placet digitalt numbern’)for i in range(len(list1)):for j in range(len(list1[i])):output.write(str(list1[i][j])+’ ’)output.write(’t’)output.write(’n’)output.close() if __name__ == ’__main__’:main()
到此這篇關(guān)于python學(xué)習(xí)將數(shù)據(jù)寫入文件并保存方法的文章就介紹到這了,更多相關(guān)python將數(shù)據(jù)寫入文件并保存詳解內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明3. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題4. PHP設(shè)計模式中工廠模式深入詳解5. CSS hack用法案例詳解6. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析7. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向8. 利用promise及參數(shù)解構(gòu)封裝ajax請求的方法9. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測可用)10. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法
