Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are just trying to get a pretty string representation of your array, and using a string array type doesn't give you the representation you want, then a message serialization format is not the thing to use. Serialization formats are for saving/transmitting data. Json is nice in that it is often human readable too, but that is not the purpose, and forcing it into a different format would make it no longer a json serialization. Even the savetxt and loadtxt numpy options are not going to work for the formatting that you want (repeating the first row for each column). You can make your own serialization if it has to be in that format using the following code:</p> <pre><code>def prettySerialize(inArray): ids = inArray[0] strRep = '' for row in inArray[1:]: for i,item in enumerate(row): rowStr = id[i] + ':' + item + '\n' strRep += rowStr return strRep </code></pre> <p>The problem with this is that it will be much slower and a much larger representation of the array (repeating the "id" row over and over). I would highly recommend going with a pure json (or msgpack) solution unless you are specifically formatting this for human reading...</p> <p>This is a solution I am using for serializing with msgpack (that will also work with json)... Convert to a tuple that includes dtype and array shape:</p> <pre><code>def arrayToTuple(arr): if arr is None: return None return (arr.dtype.str, arr.shape, arr.tostring()) def arrayFromTuple(tupl): if tupl is None: return None typeStr, shape, dataStr = tupl resultArray = numpy.fromstring(dataStr, dtype=typeStr).reshape(shape) return resultArray </code></pre> <p>So the dumps and loads commands would be:</p> <pre><code>strRep = json.dumps(arrayToTuple(arr)) arrayFromTuple(json.loads(strRep)) </code></pre> <p>And this also works for msgpack.dumps and msgpack.loads (a faster more compact binary representation).</p> <p>A caveat that may be applicable to your array: If your numpy array is an object dtype then it will not serialize by standard methods as a full array. You would have to serialize each object individually because its the object id, not data, that's stored in the array. Using dtype as dtype='|S' where is the maximum string length will make the array serializable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload