當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。