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)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ajax請(qǐng)求添加自定義header參數(shù)代碼2. ASP基礎(chǔ)知識(shí)VBScript基本元素講解3. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟4. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫5. Python requests庫參數(shù)提交的注意事項(xiàng)總結(jié)6. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式7. 利用CSS3新特性創(chuàng)建透明邊框三角8. asp知識(shí)整理筆記4(問答模式)9. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問題……10. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法
