Python實踐之路4——實現進度條和檔案內容參數替換

來源:互聯網
上載者:User

標籤:author   pos   from   style   iso   歌曲   通過   運行   was   

1、檔案進度條

代碼需求: 

實現可視化,不斷增加#####的功能。

代碼實現:

1 #!/user/bin/env ptyhon2 # -*- coding:utf-8 -*-3 # Author: VisonWong4 5 import sys,time6 for i in range(20):7     sys.stdout.write("#")8     sys.stdout.flush()9     time.sleep(0.2)

輸出結果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/進度條.py2 ####################

會有不斷增加#的動畫效果。

2、檔案內容參數替換

 代碼需求: 

通過運行相應方法,輸入替換內容和被替換內容,實現檔案內容的替換

 1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4  5 from sys import argv 6  7 script,find_str,replace_str = argv 8  9 f = open(‘yesterday.txt‘,‘r‘,encoding=‘utf-8‘)10 f1 = open(‘yesterday_bak.txt‘,‘w‘,encoding=‘utf-8‘)11 12 13 for line in f:14     if find_str in line:15         line = line.replace(find_str,replace_str)16     f1.write(line)17 18 f.close()19 f1.close()

 

 輸出結果:

運行時,在命令列輸入:

python changefile.py young old

原檔案內容:

 1 Oh, yesterday when I was young 2 噢 昨日當我年少輕狂 3 So many, many songs were waiting to be sung 4 有那麼那麼多甜美的曲兒等我歌唱 5 So many wild pleasures lay in store for me 6 有那麼多肆意的快樂等我享受 7 And so much pain my eyes refused to see 8 還有那麼多痛苦 我的雙眼卻視而不見 9 There are so many songs in me that won‘t be sung10 我有太多歌曲永遠不會被唱起11 I feel the bitter taste of tears upon my tongue12 我嘗到了舌尖淚水的苦澀滋味13 The time has come for me to pay for yesterday14 終於到了付出代價的時間 為了昨日15 When I was young16 當我年少輕狂

 

修改後檔案內容:

 1 Oh, yesterday when I was old 2 噢 昨日當我年少輕狂 3 So many, many songs were waiting to be sung 4 有那麼那麼多甜美的曲兒等我歌唱 5 So many wild pleasures lay in store for me 6 有那麼多肆意的快樂等我享受 7 And so much pain my eyes refused to see 8 還有那麼多痛苦 我的雙眼卻視而不見 9 There are so many songs in me that won‘t be sung10 我有太多歌曲永遠不會被唱起11 I feel the bitter taste of tears upon my tongue12 我嘗到了舌尖淚水的苦澀滋味13 The time has come for me to pay for yesterday14 終於到了付出代價的時間 為了昨日15 When I was old16 當我年少輕狂

 

 

Python實踐之路4——實現進度條和檔案內容參數替換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.