Skip to main content
 首页 » 编程设计

python之如何在Python中运行一段代码,类似于Matlab

2025年05月04日12artech

我的代码包括 1) 加载数据,以及 2) 处理数据。

# Load data 
import geoio.vtio as vtio 
(traces, params)=vtio.vt_read("../Seismic/R2771_10PRDMkf_Full_T_Rzn_RMO.vt") 
 
# Do things with the data 
N=params['num_samples'] 
dt=params['digi'] 

通常,当我运行 Python 脚本时,我只需在空闲状态下按“F5”,重新运行之前的任何计算都不会花费太长时间。然而,在这种情况下,数据加载需要几秒钟,我只想运行附加命令,类似于在 Matlab 中运行 m 文件中的一段代码。这可能吗?

请您参考如下方法:

如果你真的谈论iPython

学习 iPython 好东西

使用 ? 开始

>>> ? 
IPython -- An enhanced Interactive Python 
========================================= 
 
IPython offers a combination of convenient shell features, special commands 
and a history mechanism for both input (command history) and output (results 
caching, similar to Mathematica). It is intended to be a fully compatible 
replacement for the standard Python interpreter, while offering vastly 
improved functionality and flexibility. 
 
At your system command line, type 'ipython -h' to see the command line 
options available. This document only describes interactive features. 
 
MAIN FEATURES 
------------- 
 
* Access to the standard Python help. As of Python 2.1, a help system is 
  available with access to object docstrings and the Python manuals. Simply 
  type 'help' (no quotes) to access it. 
 
* Magic commands: type %magic for information on the magic subsystem. 
 
* System command aliases, via the %alias command or the configuration file(s). 
 
* Dynamic object information: 
 
  Typing ?word or word? prints detailed information about an object.  If 
  certain strings in the object are too long (docstrings, code, etc.) they get 
  snipped in the center for brevity. 
 
  Typing ??word or word?? gives access to the full information without 
  snipping long strings. Long strings are sent to the screen through the less 
  pager if longer than the screen, printed otherwise. 
  ....etc.... 

学习%magic

>>> %magic     
IPython's 'magic' functions 
=========================== 
 
The magic function system provides a series of functions which allow you to 
control the behavior of IPython itself, plus a lot of system-type 
features. There are two kinds of magics, line-oriented and cell-oriented. 
 
Line magics are prefixed with the % character and work much like OS 
command-line calls: they get as an argument the rest of the line, where 
arguments are passed without parentheses or quotes.  For example, this will 
time the given statement:: 
 
        %timeit range(1000) 
 
Cell magics are prefixed with a double %%, and they are functions that get as 
an argument not only the rest of the line, but also the lines below it in a 
separate argument.  These magics are called with two arguments: the rest of the 
call line and the body of the cell, consisting of the lines below the first. 
For example:: 
 
        %%timeit x = numpy.random.randn((100, 100)) 
        numpy.linalg.svd(x) 
 
will time the execution of the numpy svd routine, running the assignment of x 
as part of the setup phase, which is not timed. 
...etc... 

我的最爱

  • %edit - 在外部编辑器中编辑某些内容(前几行、函数、我的文件)
  • %load - 从文件加载数据,让你添加一些文本,然后运行它
  • %debug - 调试崩溃的脚本
  • %history - 显示你到目前为止做了什么
  • %paste - 从剪贴板粘贴并运行代码
  • %macro - 这可能是您问题的解决方案
  • %timeit - 测量执行时间
  • %recall - 也接近你的问题
  • %rerun - 也试试

总的来说,如果我浏览 iPython 的帮助,我总能找到一些提高生产力的方法。也试试吧。

注意:我从控制台使用 iPyhton,但其他 UI 版本即使不完全相同,也应提供非常相似的功能。