[Numpy-discussion] "ValueError: total size of new array must be unchanged" only on Windows

Chris Barker chris.barker at noaa.gov
Mon Feb 6 11:41:19 EST 2012


On Sun, Feb 5, 2012 at 10:41 AM, Paolo <p.zaffino at yahoo.it> wrote:

>  I solved using 'rb' instead of 'r' option in the open file task.
>
> that would do it, if it's binary data, but you might as well so it
"right":

> matrix="".join(f.readlines())
>
> readlines is giving you a list of the data, as separated by newline
charactors ("\n") -- it was broken on Windows, because opening the file in
text mode translated Windows newlines ("\r\n") to *nix style ones --
opening it in binary fixed that, but why use readlines at all? That's for
text -- use read. However, even better is to use fromfile(), which creates
an array form binary data in a file without puttin git in a string, first:

>  matrix = np.fromfile(f, dtype=np.int16)

by the way -- be careful of endian issues here -- if you are moving data
among different machines. You could specify the endian-ness, for instance:

dt = numpy.dtype('<i2')

for a 16 bit little-endian integer.

-CHB




-Chris

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120206/2b3483d7/attachment.html>


More information about the NumPy-Discussion mailing list