這篇文章給大家分享的是有關python中文件寫入write()怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內容如下
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'來提醒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()
訓練:
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()感謝各位的閱讀!關于“python中文件寫入write()怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。