简体   繁体   English

使用 Python 中的模块 PICKLE 打开 Matlab file.mat

[英]Open Matlab file .mat with module PICKLE in Python

I want open a Matlab project with the module Pickle or cPickle in Python language .我想用Python 语言的模块PicklecPickle打开一个Matlab项目。 NOT with:不与:

from scipy.io import matlab
mat=matlab.loadmat('file.mat')

Can I use pickle.load with a.mat file?我可以将pickle.load与 .mat 文件一起使用吗?

For some years now, Matlab has used HDF5 to store data.几年来,Matlab 一直使用 HDF5 来存储数据。 Python has support for HDF5, via PyTables . Python 通过PyTables支持 HDF5。 No need to use Pickle.无需使用泡菜。 In fact, HDF5 may surprise you for its speed relative to Pickle.事实上,HDF5 相对于 Pickle 的速度可能会让您大吃一惊。 A friend reported 2-10X speedups in read/write for some very large datasets.一位朋友报告说,对于一些非常大的数据集,读/写速度提高了 2-10 倍。


Update 1: A concise guide to loading the files, via HDF5, can be found at this page .更新 1:可以在此页面找到通过 HDF5 加载文件的简明指南。

In addition, several good references and resources may be found at this page .此外,可以在此页面上找到一些很好的参考资料和资源。 There's also a PyMat project on Sourceforge . Sourceforge 上还有一个 PyMat 项目

You can't.你不能。 Pickle loads Python objects that have been serialized to binary data. Pickle加载已序列化为二进制数据的 Python 对象。 The format is nothing like the Matlab file format.该格式与 Matlab 文件格式完全不同。

If you have read all the data you need out of the matlab file and stored it in Python objects, you can then store it for later use by Pickling it if necessary.如果您已从 matlab 文件中读取所需的所有数据并将其存储在 Python 对象中,则可以在必要时通过酸洗将其存储以供以后使用。

This is not possible.这是不可能的。 From the pickle python documentation来自泡菜python 文档

The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. pickle 模块实现了一种基本但强大的算法,用于序列化和反序列化 Python object 结构。 “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” 1 or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”.酸洗(和解酸)也称为“序列化”、“编组” 1或“扁平化”,但是,为避免混淆,这里使用的术语是“酸洗”和“解酸”。

In your case you could load the *.mat object with scipy.io and then serializing it in some python structure that you may define.在您的情况下,您可以使用 scipy.io 加载 *.mat object,然后在您可以定义的一些 Z23EEEB4347BDD25DZB 结构中对其进行序列化。 At that point you will be able to easily pickle and unpickle it.那时,您将能够轻松地腌制和解开它。 (but this last step depends, and in some use case it is not worth to be done). (但这最后一步取决于,在某些用例中不值得这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM