可愛的Python_課後習題_CDay?4 可用的首個Python 指令碼

來源:互聯網
上載者:User

標籤:can   with   blog   new   turn   readlines   join   內容   this   

  • 讀取檔案cdays?4-test.txt 內容,去除空行和注釋行後,以行為單位進行排序,並將結果輸出為cdays?4-result.txt。

  cdays?4-test.txt的內容

#some wordsSometimes in life,You find a special friend;Someone who changes your life just by being part of it.Someone who makes you laugh until you can‘t stop;Someone who makes you believe that there really is good in the world.Someone who convinces you that there really is an unlocked door just waiting for you to open it.This is Forever Friendship.when you‘re down,and the world seems dark and empty,Your forever friend lifts you up in spirits and makes that dark and emptyworldsuddenly seem bright and full.Your forever friend gets you through the hard times,the sad times,and theconfused times.If you turn and walk away,Your forever friend follows,If you lose you way,Your forever friend guides you and cheers you on.Your forever friend holds your hand and tells you that everything is goingto be okay.

cdays?4-result.txt的內容

If you lose you way,If you turn and walk away,Someone who changes your life just by being part of it.Someone who convinces you that there really is an unlocked door just waiting Someone who makes you believe that there really is good in the world.Someone who makes you laugh until you can‘t stop;Sometimes in life,This is Forever Friendship.You find a special friend;Your forever friend follows,Your forever friend gets you through the hard times,the sad times,and theYour forever friend guides you and cheers you on.Your forever friend holds your hand and tells you that everything is goingYour forever friend lifts you up in spirits and makes that dark and emptyand the world seems dark and empty,confused times.for you to open it.suddenly seem bright and full.to be okay.when you‘re down,world


python程式

#-*- coding:utf-8 -*-import osif __name__ == "__main__":    rFile = open(u"E:/cdays-4-test.txt", "r")    wFile = open(u"E:/cdays-4-result.txt", "w")    lines = rFile.readlines()    newlines = list()    for line in lines:        #去掉空行和注釋        if line == "\r\n" or line == "\n" or line.startswith("#"):            continue        newlines.append(line)    #按照行進行排序    newlines.sort()    wFile.write("%s" % "".join(newlines))    rFile.closed    wFile.closed

 

可愛的Python_課後習題_CDay?4 可用的首個Python 指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.