Python simplejson协调问题

时间:2014-01-06 04:01:29

标签: python simplejson

问题是,在我的笔记本电脑中,我有一些版本的Simplejson的python 2.7.5,在我的Debian 6服务器上,我有一些带有一些simplejson版本的Python 2.6.6。 但是在debian服务器上发生的事情是,simplejson正在为坐标值添加额外的精度 -

>>> import simplejson as json
>>> streamer_data = json.loads('{"text": "test","geo": {"type": "Point","coordinates":  [52.68908263, -8.50845340]},"coordinates": {"type": "Point","coordinates": [-8.50845340, 52.68908263]}}');

>>> print streamer_data
{u'text': test', u'geo': {u'type': u'Point', u'coordinates': [52.689082630000001, -8.5084534000000005]}, u'id': 420024061457346560L, u'coordinates': {u'type': u'Point', u'coordinates': [-8.5084534000000005, 52.689082630000001]}}

在我的笔记本电脑上,这给出了正确的结果,并且具有适当的精度来协调值 -

>>> print streamer_data
{'text': 'test', 'geo': {'type': 'Point', 'coordinates': [52.68908263, -8.5084534]}, 'coordinates': {'type': 'Point', 'coordinates': [-8.5084534, 52.68908263]}}

这是Simplejson版本控制问题还是其他问题。另请注意,我试图在debian服务器上找出simplejson的版本,但没有成功。

2 个答案:

答案 0 :(得分:4)

Python 2.6和2.7之间的这种差异与simplejson无关。在2.7中有changes to the algorithms used to produce the string representation and rounding of floating point numbers

$ python2.6 -c "print([52.68908263, -8.50845340])"
[52.689082630000001, -8.5084534000000005]
$ python2.7 -c "print([52.68908263, -8.50845340])"
[52.68908263, -8.5084534]

答案 1 :(得分:0)

@Ned的回答是正确的,但我想补充一下:

您的问题只是一个视觉问题,两个表示在“普通”计算机上完全相同两者也不完全是计算机(可以)存储的内容

>>> a = 52.68908263
>>> b = 52.689082630000001
>>> '{0:.30} = {1:.30}: {2}'.format(a, b, a==b)
Python 2.6:
'52.6890826300000014725810615346 = 52.6890826300000014725810615346: True'
Python 2.7:
'52.6890826300000014725810615346 = 52.6890826300000014725810615346: True'

现在可见:这些数字之间没有任何区别。 Python2.7打印较短的数字表示,当它可以确保它们回滚到相同的内部值。 Python2.7只能在某些系统(有一些编译器)上执行此操作(检查sys.float_repr_style,它是“短”或“遗留”)。

相关问题