当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python numpy DataSource用法及代码示例

本文简要介绍 python 语言中 numpy.DataSource 的用法。

用法:

class  numpy.DataSource(destpath='.')

通用数据源文件(文件、http、ftp、...)。

DataSources可以是本地文件或远程文件/URL。文件也可以是压缩的或未压缩的。 DataSource隐藏了下载文件的一些低级细节,允许您简单地传入有效的文件路径(或URL)并获取文件对象。

参数

destpath str 或无,可选

源文件下载到以供使用的目录的路径。如果 destpath 为 None,将创建一个临时目录。默认路径是当前目录。

注意

URL 需要使用方案字符串(http://),没有它它们将失败:

>>> repos = np.DataSource()
>>> repos.exists('www.google.com/index.html')
False
>>> repos.exists('http://www.google.com/index.html')
True

删除 DataSource 时,临时目录也会被删除。

例子

>>> ds = np.DataSource('/home/guido')
>>> urlname = 'http://www.google.com/'
>>> gfile = ds.open('http://www.google.com/')
>>> ds.abspath(urlname)
'/home/guido/www.google.com/index.html'

>>> ds = np.DataSource(None)  # use with temporary file
>>> ds.open('/home/guido/foobar.txt')
<open file '/home/guido.foobar.txt', mode 'r' at 0x91d4430>
>>> ds.abspath('/home/guido/foobar.txt')
'/tmp/.../home/guido/foobar.txt'

相关用法


注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.DataSource。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。