在Google Colab中从github读取.mat文件 [英] Read .mat file from github in Google colab

查看:235
本文介绍了在Google Colab中从github读取.mat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从github链接中读取.mat文件( https://github.com/pranavn91/APS/blob/master/data.mat )并将其存储到变量中.我正在使用python3和google colab.

I want to read a .mat file from a github link (https://github.com/pranavn91/APS/blob/master/data.mat) and store it into a variable. I am using python3 and google colab.

第一种方法:

!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正确

Second method: FileNotFoundError but file url is correct

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'

推荐答案

下面是一个示例:

https://colab.research.google.com/drive/1GGyNoKYHECmf-vlH3AZdTsGYKg

相关位:

# 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')

这篇关于在Google Colab中从github读取.mat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆