Python 从Google colab中的github读取.mat文件
我想从github链接()读取一个.mat文件,并将其存储到一个变量中。我正在使用python3和googlecolab 第一种方法:Python 从Google colab中的github读取.mat文件,python,google-colaboratory,Python,Google Colaboratory,我想从github链接()读取一个.mat文件,并将其存储到一个变量中。我正在使用python3和googlecolab 第一种方法: !wget http://upscfever.com/upsc-fever/en/data/deeplearning2/images/data.mat -P drive/app f = h5py.File("drive/app/data.mat", "r") data = f.get('data/variable1') data = np.arra
!wget http://upscfever.com/upsc-fever/en/data/deeplearning2/images/data.mat -P drive/app
f = h5py.File("drive/app/data.mat", "r")
data = f.get('data/variable1')
data = np.array(data)
第二种方法:FileNotFoundError,但文件url是正确的
url = 'https://github.com/pranavn91/APS/blob/master/data.mat'
import scipy.io
mat = scipy.io.loadmat(url)
错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py in _open_file(file_like, appendmat)
32 try:
---> 33 return open(file_like, 'rb'), True
34 except IOError:
FileNotFoundError: [Errno 2] No such file or directory: 'https://github.com/pranavn91/APS/blob/master/data.mat'
During handling of the above exception, another exception occurred:
FileNotFoundError Traceback (most recent call last)
<ipython-input-2-ee625cac9f79> in <module>()
1 url = 'https://github.com/pranavn91/APS/blob/master/data.mat'
2 import scipy.io
----> 3 mat = scipy.io.loadmat(url)
/usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py in loadmat(file_name, mdict, appendmat, **kwargs)
139 """
140 variable_names = kwargs.pop('variable_names', None)
--> 141 MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)
142 matfile_dict = MR.get_variables(variable_names)
143 if mdict is not None:
/usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py in mat_reader_factory(file_name, appendmat, **kwargs)
62
63 """
---> 64 byte_stream, file_opened = _open_file(file_name, appendmat)
65 mjv, mnv = get_matfile_version(byte_stream)
66 if mjv == 0:
/usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py in _open_file(file_like, appendmat)
37 if appendmat and not file_like.endswith('.mat'):
38 file_like += '.mat'
---> 39 return open(file_like, 'rb'), True
40 else:
41 raise IOError('Reader needs file name or open file-like object')
FileNotFoundError: [Errno 2] No such file or directory: 'https://github.com/pranavn91/APS/blob/master/data.mat'
---------------------------------------------------------------------------
FileNotFoundError回溯(最近一次调用上次)
/打开文件中的usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py(类似文件,附录)
32试试:
--->33返回open(类似于“rb”的文件),True
34除IOError外:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'https://github.com/pranavn91/APS/blob/master/data.mat'
在处理上述异常期间,发生了另一个异常:
FileNotFoundError回溯(最近一次调用上次)
在()
1 url=https://github.com/pranavn91/APS/blob/master/data.mat'
2导入scipy.io
---->3 mat=scipy.io.loadmat(url)
/loadmat中的usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py(文件名、MDCT、appendmat、**kwargs)
139 """
140 variable_names=kwargs.pop('variable_names',无)
-->141 MR,文件打开=材料读取器工厂(文件名,附录,**kwargs)
142 matfile\u dict=MR.get\u变量(变量名称)
143如果MDCT不是无:
/材料阅读器工厂中的usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py(文件名,附录,**kwargs)
62
63 """
--->64字节\u流,文件\u打开=\u打开\u文件(文件名,附录)
65 mjv,mnv=获取数据文件版本(字节流)
66如果mjv==0:
/打开文件中的usr/local/lib/python3.6/dist-packages/scipy/io/matlab/mio.py(类似文件,附录)
37如果是appendmat而不是类似.endswith('.mat')的文件:
38类文件+='.mat'
--->39返回open(类似于“rb”的文件),True
40其他:
41 raise IOError('读取器需要文件名或像对象一样打开文件')
FileNotFoundError:[Errno 2]没有这样的文件或目录:'https://github.com/pranavn91/APS/blob/master/data.mat'
以下是一个示例:
相关位:
# raw=true is important so you download the file rather than the webpage.
!wget https://github.com/pranavn91/APS/blob/master/data.mat?raw=true
# rename the file
!mv data.mat\?raw\=true data.mat
# update scipy
!pip install -U -q scipy
# Load the data
from scipy import io
v = io.loadmat('data.mat')
下面是一个例子:
相关位:
# raw=true is important so you download the file rather than the webpage.
!wget https://github.com/pranavn91/APS/blob/master/data.mat?raw=true
# rename the file
!mv data.mat\?raw\=true data.mat
# update scipy
!pip install -U -q scipy
# Load the data
from scipy import io
v = io.loadmat('data.mat')
是什么让你认为它可以打开url的?我似乎没有在文档中看到它。wget在colabI中工作,我不知道这如何回答我的问题。我问的是第二种方法。如果您选择下载文件的位置(如第一种方法),并使用
scipy.io.loadmat
加载它,那么它应该可以工作。但是,loadmat
不从url加载什么使您认为它可以打开url?我似乎没有在文档中看到它。wget在colabI中工作,我不知道这如何回答我的问题。我问的是第二种方法。如果您选择下载文件的位置(如第一种方法),并使用scipy.io.loadmat
加载它,那么它应该可以工作。但是loadmat
不从urlsCan加载使用curl
和-o在一步中重命名。高超的技术技能可以使用curl
和-o在一步中重命名。高超的技术技能