Drkcore

13 02 2012 perl Python JSON Tweet

parseできない浮動小数点のJSON表記

やたらとこけるJSONを出力する商用のツールを追いかけていたら、浮動小数点の表記に問題があった。1.e-2っていうのはJSON的にはmalformed numberなんだろうか?

Perl

use strict;
use warnings;
use JSON;

print decode_json('{"num": 1.e-2}')->{'num'};

実行

$ perl jsontest.pl 
malformed number (no digits after decimal point), at character offset 10 (before "e-2}") at jsontest.pl line 5.

Python

>>> import json
>>> json.loads('{"num":1.0e-2}')
{u'num': 0.01}
>>> json.loads('{"num":1e-2}')
{u'num': 0.01}
>>> json.loads('{"num":1.e-2}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 8 (char 8)

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021