Python数据分析JupyterNotebook3魔法命令详解及示例

2022-07-21,,,,

1、魔法命令介绍

%lsmagic:列出所有magics命令

available line magics:【对当前行使用共计93个】
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %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  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

%quickref:输出所有魔法指令的简单版帮助文档

%magics_name?:输出某个魔法命令详细帮助文档

魔法命令名称?输出魔法命令的详细帮助文档,以%alias为例:

2、line magics:line魔法指令

%alias:设置指令的别名

windows有8个默认的指令,功能和linux下一样。

#windows下有8个命令
in [1]: %alias#
total number of aliases: 8
out[1]:
[('copy', 'copy'),
 ('ddir', 'dir /ad /on'),
 ('echo', 'echo'),
 ('ldir', 'dir /ad /on'),#列出文件夹
 ('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'),
 ('ldir', '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'),
 ('mkdir', 'mkdir'),
 ('more', 'more'),
 ('mv', 'mv'),
 ('rm', 'rm'),
 ('rmdir', '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、javascript、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魔法命令的资料请关注其它相关文章!

《Python数据分析JupyterNotebook3魔法命令详解及示例.doc》

下载本文的Word格式文档,以方便收藏与打印。