【发布时间】:2022-01-23 01:59:17
【问题描述】:
在使用 python 3.6.8 的 linux 服务器上遇到以下问题。 带有简单浮点变量的 Json.dumps() 调用在每次运行中给出不同的结果。
$ python3
Python 3.6.8 (default, Oct 8 2020, 16:04:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.200)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> print(json.dumps(float(2341)))
2373.0
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2342.3121632
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2341.0
>>> print(json.dumps(float(2341)))
2341.0
>>> print(json.dumps(float(2341)))
2373.0
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.__file__)
/usr/lib64/python3.6/json/__init__.py
>>> exit()
$ which python3
/usr/bin/python3
$
'Int' 变量没有问题。 已验证 python 包文件夹中的文件,一切看起来都完好无损。 没有发现与服务器内存/cpu 相关的问题。 重新启动服务器/重新安装 python 包目前不是可行的选项。 在进一步调试/修复此问题时需要一些指示。
---- 编辑 ---- print(float(x)) 也出现了问题
$ python3
Python 3.6.8 (default, Oct 8 2020, 16:04:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.200)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(float(2341))
2341.0
>>> print(float(2341))
2341.0
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.312
>>> print(float(2341))
2342.3121632
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.312
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.312
>>> print(float(2341))
2342.28
>>> print(float(2341))
2341.0
>>> print(float(2341))
2373.0
>>> exit()
----- EDIT ---- 不同风格的 print + float。同样的问题。
>>> x = float(2341)
>>> for _ in range(20): print(x)
...
2342.312
2342.28
2342.28
2342.28
2342.312
2342.312
2373.0
2342.28
2373.0
2373.0
2373.0
2373.0
2342.3121632
2342.3121632
2373.0
2342.28
2373.0
2342.28
2342.28
2373.0
>>>
>>> for _ in range(20): print(2341.0)
...
2373.0
2342.28
2342.312
2342.28
2342.28
2342.28
2342.28
2373.0
2342.28
2342.28
2373.0
2373.0
2342.28
2373.0
2342.312
2373.0
2373.0
2342.28
2342.3121632
2342.3121632
>>>
>>> import builtins
>>> print(builtins.float(2341))
2342.28
>>> print(builtins.float(2341))
2373.0
>>> print(builtins.float(2341))
2342.3121632
>>> print(builtins.float(2341))
2373.0
>>>
【问题讨论】:
-
print(float(2341))工作正常吗?x = float(2341)for _ in range(10): print(x, json.dumps(x))的输出是什么? -
无法复现,导致您认为您的包有问题。
-
除了无法在运行 Windows 10 的 Python 3.9.6 上重现您的问题外,我没有任何帮助。希望您能解决这个问题。
-
这在 Python 的正常安装中是无效行为。 也许某些代码劫持了名称
float或带有“娱乐”版本的 json 模块? -
我的其他测试怎么样,多次打印同一个浮动对象?
标签: python json python-3.x