Python資料分析JupyterNotebook3魔法命令詳解及示例
目錄
- 1、魔法命令介紹
- %lsmagic:列出所有magics命令
- %quickref:輸出所有魔法指令的簡單版幫助文件
- %Magics_Name?:輸出某個魔法命令詳細幫助文件
- 2、Line magics:Line魔法指令
- 3、Cell magics:Cell魔法指令
- 寫bash程式
- 寫perl程式
1、魔法命令介紹
%lsmagic:列出所有magics命令
Available line magics:【對當前行使用共計93個】 %alias %alias_magic %autoawait %autocall %automagic %autosave %bwww.cppcns.comookmark %cd %clear %cls %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics:【對當前cell使用共計28個】 %%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %% %% %%latex %%markdown %%perl %%prun %%pypy %% %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
%quickref:輸出所有魔法指令的簡單版幫助文件
%Magics_Name?:輸出某個魔法命令詳細幫助文件
魔法命令名稱?輸出魔法命令的詳細幫助文件,以%alias為例:
2、Line magics:Line魔法指令
%alias
:設定指令的別名
windows有8個預設的指令,功能和下一樣。
#Windows下有8個命令
In [1]: %alias#
Total number of aliases: 8
Out[1]:
www.cppcns.com[('copy','copy'),('ddir','dir /ad /on'),('echo','echo'),('ldir',#列出資料夾
('ls','dir /on'),('mkdir','mkdir'),#建立資料夾
('ren','ren'),('rmdir','rmdir')]#刪除資料夾
Linux下有16個預設指令,感興趣可自己試驗。
In [3]: %alias Total number of aliases: 16 Out[3]: [('cat','cat'),('clear','clear'),('cp','cp'),'ls -F -o --color %l | grep /$'),('less','less'),('lf','ls -F -o --color %l | grep ^-'),('lk','ls -F -o --color %l | grep ^l'),('ll','ls -F -o --color'),('ls','ls -F --color'),('lx','ls -F -o --color %l | grep ^-..x'),('man','man'),('more','more'),('mv','mv'),('rm','rm'),'rmdir')]
自己設定指令的別名,個人感覺沒啥意義,介紹一個。
%conda
:cell中安裝packageM
%conda install package_names
%dhist
:輸出歷史訪問目錄
%history
:列出歷史輸入的指令
效果類似linux中history。
%magic
:輸出所有魔法指令幫助文件
%matplotlib inline
:效果等價於plt.show()
%notebook
:匯出當前notebook所有歷史輸入到一個檔案中
%notebook notebook.ipynb將所有歷史輸入匯入notebook.ipynb檔案中
%pip
:在cell中使用pip指令
%pwd
:輸出當前路徑
%pycat
:預覽檔案,類似linux中cat
%run
:執行
%time
:執行時間
3、Cell magics:Cell魔法指令
%%writefile
:將當前cell中內容寫入檔案中
%%latex
:寫Latex公式
%%latex \begin{equation} \int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15} \label{eq:sample} \end{equation}
%%script
:寫bash、perl、script、js 等命令
這個可以使用bash、perl、javascript、js 等等,不過經過測試,在jupyter notebook中不友好,在ipython中沒什麼問題。
以下在ipython中完成:
寫bash程式
In [9]: %%script bash ...: for i in 1 2 3; do ...: echo $i; ...: done 1 2 3
寫perl程式
In [11]: %%script perl ...: print "hhn"; hhn
寫python2程式
In [12]: %%script python2 ...: print "hhhn" ...: ...: hhhn
參考資料:https://ipython.readthedocs.io/en/stable/interactive/magics.html#
以上就是JupyterNotebook3魔法命令詳解及示例的詳細內容,更多關於JupyterNotebook3魔法命令的資料請http://www.cppcns.com關注我們其它相關文章!