如何避免在mjson.tool中进行实数转换

编程入门 行业动态 更新时间:2024-10-26 08:31:28
本文介绍了如何避免在mjson.tool中进行实数转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现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答案: stackoverflow/a/18936966/956660

To avoid the scientifique notation, see @Veedrac answer on the subject: stackoverflow/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中进行实数转换

本文发布于:2023-10-28 05:16:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实数   mjson   tool

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!