python write 写多行_如何用 Python 执行单行命令
一般來說,面對日常處理的一些小任務,直接用?sed,grep?之類的就可以搞定,更復雜一點的就會考慮?awk?或者用一些現成的輪子,要是?awk?搞不定我就只好用 Python 了。但有些時候,我僅僅只是想寫一個一次性腳本,不想打開編輯器寫所謂?格式優美?的 Python 語句呢?(正如寫 Perl 一樣隨性)
其實在 Python 中也可以使用單行命令,直接用?python -c ?即可,雖然這與 Python 之禪中所說的?Readability counts?相悖,但信手拈來,隨便用用還是挺方便的。
-c cmd : program passed in as string (terminates option list)terminates option list 表示?-c?之后的其它選項不起作用,為終極選項。
例如:
python -c "print('Hello World')"Hello World-c?之后,要用雙引號將命令包起來,import?以;結尾,命令用[]括起來,多行命令用多個[]。
python -c "import os,time;[print(i) for i in os.listdir()];[print(time.time())]"一些比較復雜的命令復雜的命令必須要用[]括起來,否則會報錯。
舉個實際的例子,比如在生物信息學編程實戰一文中第三題 ,hg19 基因組序列的一些探究,jimmy 老師用 Perl 單行命令做了這道題:
perl -alne '{if(/^>/){$chr=$_}else{ $A_count{$chr}+=($_=~tr/Aa//); $T_count{$chr}+=($_=~tr/Tt//);$C_count{$chr}+=($_=~tr/Cc//); $G_count{$chr}+=($_=~tr/Gg//); $N_count{$chr}+=($_=~tr/Nn//); }}END{print "$_ $A_count{$_} $T_count{$_} $C_count{$_} $G_count{$_} $N_count{$_}" foreach sort keys %N_count}' test.fa示例數據:
>chr_1ATCGTCGaaAATGAANccNNttGTAAGGTCTNAAccAAttGggG>chr_2ATCGAATGATCGANNNGccTAAGGTCTNAAAAGG>chr_3ATCGTCGANNNGTAATggGAAGGTCTNAAAAGG>chr_4ATCGTCaaaGANNAATGANGgggTA結果如下:
>chr_1 13 10 7 10 4>chr_2 11 6 5 8 4>chr_3 10 6 3 10 4>chr_4 9 4 2 7 3這題用 Python 單行命令也可以寫:
cat test.fa | python -c "import sys;from Bio import SeqIO;[print(line.id,line.seq.count('A'),line.seq.count('T'),line.seq.count('C'),line.seq.count('G')) for line in SeqIO.parse(sys.stdin,'fasta')]"這里我就用了 Biopython 來解析 fasta 格式,所以 Python 的優勢就在于有眾多現成的模塊可以調用,減少了代碼量。
再舉個例子,比如一行代碼轉換 genbank 為 fasta :
cat sequence.gb | python -c "import sys;from Bio import SeqIO;SeqIO.write(SeqIO.parse(sys.stdin, 'genbank'),sys.stdout,'fasta')" > sequence.fasta關于更多 Biopython 的內容可以參見我之前的筆記:
?我的Python筆記·BioPython(一)?我的Python筆記·BioPython(二)?用 BioPython 做一些酷酷的事情(一)?用 BioPython 做一些酷酷的事情(二)
其他例子
?算術:
python -c "print(3.0/2)"?導入模塊并輸出結果:
python -c "import math;print(math.sin(1))"?使用循環輸出 1-10:
python -c "for i in range(1,11):print(i)"?使用多個循環(注意格式):
python -c "for i, j in ((i,j) for i in range (1,11) for j in range(1,11)): print(i, j)"?實現類似?grep?的功能,輸出正則匹配的行:
echo hey | python -c "import sys,re;[sys.stdout.write(line) for line in sys.stdin if re.search('he.', line)]"?實現類似?sed?的功能,使用正則表達式替換并輸出結果:
echo hallo | python -c "import sys,re;[sys.stdout.write(re.sub('h[au]llo', 'hello', line)) for line in sys.stdin]"?刪除前兩個字符:
python -c "import sys;[sys.stdout.write(' '.join(line.split(' ')[2:])) for line in sys.stdin]" < input.txt除了使用?-c?之外,Python 還可以使用?-m?參數直接使用模塊
-m mod : run library module as a script (terminates option list)?使用 calendar 模塊,輸出今年的日歷:
python -m calendar?開啟文件分享:
python -m http.server 8000執行后,在本機打開?http://localhost:8000?,或者在局域網內的其它機器上打開?http://本機ip:8000,就能訪問到執行目錄下的文件。
?生成 HTML 格式官方幫助文檔:
python -m pydoc -p 9000?安裝 module:
python -m pip install xxx這種寫法相比于?pip install xxx?,在存在多個 Python 版本的環境中,可以精確地控制三方庫的安裝位置。
?創建、查看和提取 zip 格式壓縮包:?-l?顯示 zip 格式壓縮包中的文件列表?-c?創建 zip 格式壓縮包?-e?提取 zip 格式壓縮包?-t?驗證文件是一個有效的 zip 格式壓縮包
python -m zipfile -c test.zip tmp1.txt tmp2.txtpython -m zipfile -e test.zip target-dir/python -m zipfile -l test.zip引用鏈接
[1]??https://en.wikibooks.org/wiki/Python_Programming/Command-line_one-liners
[2]?Python 中 -m 的典型用法、原理解析與發展演變
生信技能樹目前已經公開了三個生信知識庫,記得來關注哦~
每周文獻分享
https://www.yuque.com/biotrainee/weeklypaper
腫瘤外顯子分析指南
https://www.yuque.com/biotrainee/wes
生物統計從理論到實踐
https://www.yuque.com/biotrainee/biostat
友情宣傳
強烈建議你推薦給身邊的博士后以及年輕生物學PI,多一點數據認知,讓他們的科研上一個臺階:
?生信技能樹的2019年終總結,你的生物信息學成長寶藏?2020學習主旋律,B站74小時免費教學視頻為你領路?全國巡講全球聽(買一得五),你的生物信息學入門課
總結
以上是生活随笔為你收集整理的python write 写多行_如何用 Python 执行单行命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统下升级node,linux
- 下一篇: android 刷新view位置,And