问题描述
我发现mjson.tool使用科学计数法转换实数,例如:
I have discovered that mjson.tool converts real number using scientific notation, e.g:
$ echo '{"k":0.000000581}' | python -mjson.tool
{
"k": 5.81e-07
}
但是,我想避免这种转换,因此前者将打印为:
However, I would like to avoid this conversion, so the former get printed as:
{
"k": 0.000000581
}
可以通过这种方式使用mjson.tool吗?
It is possible to use mjson.tool in such way, please?
推荐答案
据我所见,mjson模块只是将输入转换为json,然后使用缩进和排序键将其转换回字符串.
From what I see, mjson module just convert the input to json and back to string with indentation and sorted keys.
这可以通过以下方式完成:
This can be done with:
>>> json.dumps(json.loads('{"k":0.000000581}', indent=2, sort_keys=True))
'{"k": 5.81e-07}'
为避免科学表示法,请参阅有关此主题的@Veedrac答案:
https://stackoverflow.com/a/18936966/956660
To avoid the scientifique notation, see @Veedrac answer on the subject:
https://stackoverflow.com/a/18936966/956660
编辑:任何仅重新格式化但不尝试解析/广播类型的工具都可以使用.
Edit: Any tools that only reformat and does not try to parse/cast types will work.
我尝试了 yajl-tools
:
user$ sudo apt-get install yajl-tools
user$ echo '{"a": 0.0000000000000001337}' | json_pp
{
"a" : 1.337e-16
}
user$ echo '{"a": 0.0000000000000001337}' | json_reformat
{
"a": 0.0000000000000001337
}
这篇关于如何避免在mjson.tool中进行实数转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!