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

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

PYTHON:關(guān)閉文件的 I/O 操作

PYTHON:關(guān)閉文件的 I/O 操作

元芳怎么了 2021-12-29 10:31:29
我相信這一切都與縮進(jìn)有關(guān),但我不確定如何準(zhǔn)確縮進(jìn),請(qǐng)幫助我。import csvwith open('abbreviations.csv', mode='r') as infile:   reader = csv.reader(infile)with open('abbreviations_new.csv', mode='w') as outfile:   writer = csv.writer(outfile)   mydict = {rows[0]:rows[1] for rows in reader}   print(len(mydict))   print(mydict['v'])   mydict['MIS'] = 'Management Information System'   print(mydict['TA'])   mydict['TA'] = 'teaching assistant'   print(mydict['TA'])   print(mydict['flower'])   del mydict['flower']
查看完整描述

2 回答

?
呼啦一陣風(fēng)

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

試試這個(gè)方法


import csv


with open('abbreviations.csv', mode='r') as infile:

   reader = csv.reader(infile)


   with open('abbreviations_new.csv', mode='w') as outfile:

       writer = csv.writer(outfile)


       mydict = {rows[0]:rows[1] for rows in reader}


       print(len(mydict))

       print(mydict['v'])

       mydict['MIS'] = 'Management Information System'

       print(mydict['TA'])

       mydict['TA'] = 'teaching assistant'

       print(mydict['TA'])

       print(mydict['flower'])

       del mydict['flower']

當(dāng)您使用 with open 時(shí),循環(huán)結(jié)束時(shí)文件會(huì)自動(dòng)關(guān)閉。


查看完整回答
反對(duì) 回復(fù) 2021-12-29
?
鴻蒙傳說(shuō)

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

您可以使用


with open('abbreviations.csv', mode='r') as infile, open(  # you can put two opens after 

          'abbreviations_new.csv', mode='w') as outfile:   # one with statement

    reader = csv.reader(infile)

    writer = csv.writer(outfile)


    mydict = {rows[0]:rows[1] for rows in reader}


    print(len(mydict))

    print(mydict['v'])

    mydict['MIS'] = 'Management Information System'

    print(mydict['TA'])

    mydict['TA'] = 'teaching assistant'

    print(mydict['TA'])

    print(mydict['flower'])

    del mydict['flower']


# on this indentation level the files are closed

一旦您離開with open(...) as f:文件的縮進(jìn),文件就會(huì)自動(dòng)關(guān)閉。


查看完整回答
反對(duì) 回復(fù) 2021-12-29
  • 2 回答
  • 0 關(guān)注
  • 214 瀏覽
慕課專欄
更多

添加回答

舉報(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)