【问题标题】:Error when opening .mat file in python在 python 中打开 .mat 文件时出错
【发布时间】:2018-03-30 01:01:03
【问题描述】:

我正在尝试打开一个 MATLAB 文件,它是一个“结构数组”。使用 scipy.io.loadmat 打开文件时,出现以下错误:

File "<ipython-input-15-0951b80baef6>", line 1, in <module>
    data = sio.loadmat('C:\Users\Martin\Desktop\Biophysics PhD\Results\180321_agonists_spreading_conditions\180321_agonists_spreading_conditions\Compare_ADPdexBSA.mat')

File "C:\Users\Martin\Anaconda2\lib\site-packages\scipy\io\matlab\mio.py", line 141, in loadmat
MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)

File "C:\Users\Martin\Anaconda2\lib\site-packages\scipy\io\matlab\mio.py", line 64, in mat_reader_factory
byte_stream, file_opened = _open_file(file_name, appendmat)

TypeError: 'NoneType' object is not iterable

对编程来说还不够新,所以我不确定如何解释错误。非常感谢您能给我的任何帮助

【问题讨论】:

    标签: python matlab io scipy


    【解决方案1】:

    这个错误很可能发生,因为scipy.io.loadmat 找不到感兴趣的文件。因为您使用的是 Windows,所以您定义的路径并不完全正确。您需要用两个反斜杠来划定目录分隔符\\\

    换句话说:

    data = sio.loadmat('C:\\Users\\Martin\\Desktop\\Biophysics PhD\\Results\\180321_agonists_spreading_conditions\\180321_agonists_spreading_conditions\\Compare_ADPdexBSA.mat')
    

    【讨论】:

    • 谢谢,您的解决方案有效。我不敢相信这个问题这么容易解决,即使我直接从 Windows 资源管理器地址栏中复制了地址,该地址也不起作用
    • @kynnem 当然可以!我知道这个错误,因为这曾经发生在我身上。实际上,如果您不将反斜杠加倍,则错误是未定义的,因为您正在指定文件的实际路径。在 Python 3 上,我收到一个 unicode 错误。在 Python 2 上,我收到无效模式错误。要与平台无关,请考虑使用os.path.join。这样,无论操作系统如何,它都能正确插入目录定界符:data = sio.loadmat(os.path.join('C:', 'Users', 'Martin', ...))
    • 我一般觉得pathlibos.path好用。
    • @excaza 很有趣。谢谢!
    猜你喜欢
    • 2017-10-30
    • 1970-01-01
    • 2011-04-29
    • 2019-09-25
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多