第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我如何將數(shù)據(jù)保存到文本文件中,主要是我在記錄中寫的內(nèi)容

我如何將數(shù)據(jù)保存到文本文件中,主要是我在記錄中寫的內(nèi)容

人到中年有點(diǎn)甜 2022-01-11 17:58:51
python新手,需要將此數(shù)據(jù)存儲(chǔ)到文本文件中。主要是我的記錄,但在 recsave.write 行中出現(xiàn)了 concat 錯(cuò)誤(元組)。任何幫助或建議,或者只是做錯(cuò)了print (' Title ')name = 'y'while name == 'y':    print (' Enter the name of the sender: ')    sender = input("\n")     # add name of reciever #    print (' Enter the name of the reciever: ')    reciever = input("\n")     # how much would you like to send #     print (' How much would you like to send :$ ')    amount = input("\n")    record = (sender, 'sent :$',amount, 'to', reciever, "\n" )    recsave = open('Transaction History.txt', 'w')    recsave.write(record + '\n')    recsave.close()    print (str(record, "\n"))    name = input (' Are there anymore transactions? ( Enter y or n ): ')想在打開文本文件時(shí)得到它。你得到name 發(fā)送 $amount 給 name還需要包括時(shí)間戳:(每次循環(huán)運(yùn)行時(shí)都會(huì)將每個(gè)循環(huán)保存到記錄中
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

創(chuàng)建一個(gè)包含所有信息而不是元組的字符串并寫入


import datetime


dt = datetime.datetime.now()

record = "{} : {} sent :${} to {}\n".format(dt, sender, amount, reciever)


recsave = open('Transaction History.txt', 'a')

recsave.write(record)

recsave.close()


print(record)

使用模式"a"(追加)而不是"w"(寫入)將新記錄添加到文件中的現(xiàn)有記錄。

w將刪除舊信息。


在您添加的記錄中,"\n"因此您不必添加它write()


但是您可以創(chuàng)建功能來執(zhí)行此操作,然后您可以在不同的地方使用它


import datetime


def log(message):

    dt = datetime.datetime.now()


    line = "{} : {}\n".format(dt, message)


    recsave = open('Transaction History.txt', 'a')

    recsave.write(line)

    recsave.close()


    print(line)


#----


log("start program")


record = "{} sent :${} to {}".format(dt, sender, amount, reciever)

log(record)


log("end program")

有標(biāo)準(zhǔn)模塊日志記錄來創(chuàng)建帶有歷史記錄的日志或文件。


查看完整回答
反對(duì) 回復(fù) 2022-01-11
  • 1 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)