這篇文章給大家分享的是有關(guān)python中文件寫入write()怎么用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內(nèi)容如下
filename = 'pragramming.txt' with open(filename,'w') as fileobject: #使用‘w'來提醒python用寫入的方式打開 fileobject.write('I love your name!' '\nI love your cloth!' '\nI love your shoes!' '\nI love your hair!') with open(filename,'a') as fileobject: #使用‘a(chǎn)'來提醒python用附加模式的方式打開 fileobject.write('\nI an superman.')
代碼中的filename如果沒有這個文件,python會自己新建一個。
json文件的寫入和讀?。?/p>
import json filename = 'number.json' def write_json(): numbers = [1,2,3,4,5,6,7,8,9,10] with open(filename,'w') as fp: json.dump(numbers,fp)#寫入json文件 write_json() def read_json(): with open(filename) as pf: numbers = json.load(pf)#讀取json文件 print(numbers) read_json()
訓(xùn)練:
import json def remember_me(): active = True while active: for i in range(5): if i < 4: username = input('Please enter your name:') filename = 'name.json' with open(filename,'w') as fp:#以w的方式打開寫入時會覆蓋原有記錄,而以a打開不會 json.dump(username,fp) print('Hello! '+username.title()) else: active = False remember_me()
感謝各位的閱讀!關(guān)于“python中文件寫入write()怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
新聞標(biāo)題:python中文件寫入write()怎么用-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://chinadenli.net/article48/dgjcep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、微信公眾號、網(wǎng)站排名、關(guān)鍵詞優(yōu)化、響應(yīng)式網(wǎng)站、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)