[SciPy-User] sugestion for loadmat (scipy.io)

Matthew Brett matthew.brett@gmail....
Thu Sep 8 11:02:36 CDT 2011


On Thu, Sep 8, 2011 at 3:34 AM, Roel De Coninck <roeldeconinck@gmail.com> wrote:
> Hi,
> I spent a few hours trying to read some .mat files.
> The first error I got was a UnicodeDecodeError caused by
> VarReader4.read_char_array().  I could solve this by replacing
> S = arr.tostring().decode('ascii')
> by
> S = arr.tostring().decode('ascii', 'ignore')
> Then, I got this error, caused by the same method read_char_array():
> TypeError: buffer is too small for requested array
> (full error message see below)
> I could solve this after trying lots of things, by changing
> S = arr.tostring().decode('ascii', 'ignore')
> into
> S = arr.tostring().decode('ascii', 'replace')
> It also works with S = arr.tostring().decode('utf-8', 'replace'), which is
> maybe even better as ascii.
> Is there a drawback to what I did that I did not yet discover?
> Else, I would suggest to add 'replace' to avoid UnicodeDecodeError in this
> method.

I guess you've got characters above ord(127) in your mat file strings,
and I hadn't hit that before.

I think the correct fix in both cases is this:

S = arr.tostring().decode('latin1')

- does that work?

Have you got a small .mat file you can send to me off-list that I can
test against?



More information about the SciPy-User mailing list