Numpy, hepsi listed here olan çok farklı temel türlere sahiptir.Neden bazı numpy veri tabanları JSON serileştirilebilir ve diğerleri değil?
JSON serileştirilebilir olmama s float32
benim programda bir sorunu izini, o yüzden yukarıdaki listeden tüm veri tiplerini test başladım: Yani
>>> import numpy as np
>>> from json import dumps
>>> dumps(np.bool(True))
'true'
>>> dumps(np.bool_(True))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: True is not JSON serializable
>>> dumps(np.int(0))
'0'
>>> dumps(np.int_(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.intc(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.intp(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int8(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int16(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int32(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int64(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint8(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint16(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint32(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint64(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.float(0))
'0.0'
>>> dumps(np.float_(0))
'0.0'
>>> dumps(np.float16(0))
Traceback (most recent call last):
[...]
TypeError: 0.0 is not JSON serializable
>>> dumps(np.float32(0))
Traceback (most recent call last):
[...]
TypeError: 0.0 is not JSON serializable
>>> dumps(np.float64(0))
'0.0'
>>> dumps(np.complex(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex_(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex64(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex128(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable
, hiçbir complex
tip seri hale getirilebilir , bu mantıklı.
bool
ve bool_
çalışmıyor. int
çalışır, ancak adında int
ile başka bir şey yok. float
, float_
ve float64
tüm iyi, ancak float16
ve float32
değil.
Bu neden böyle? Açıkçası, hepsi kolayca bir dizeye dönüştürülebilir, stacktrace bile değerlerini göstermek için kullanılan repr()
gösterir. Bu durumun kasıtsız olabilmesi mümkün mü? Yoksa bu davranış için iyi bir neden var mı? serializable JSON olan
Doğrudan 'np.float32 (...)' nesnesini mi oluşturuyorsunuz? Yoksa bir diziden mi çıkarıldı? 'Arr.tolist() 'eşdeğerini serileştirmeyi denediniz mi? – hpaulj
Dizinin bir elemanını serileştirmeniz gerekiyorsa, bir Python skalar dosyasına dönüştürmek için '.item()' yöntemini kullanmayı düşünün. – hpaulj
Ayrıca bkz. Https://stackoverflow.com/questions/27050108/convert-numpy-type-to-python 'Numune türünü python'a dönüştür ' – hpaulj