2014-01-06 36 views
1

問題是,在我的筆記本電腦中,我有一些版本的Simplejson和我的Debian 6服務器的Python 2.7.5我有Python 2.6.6一些simplejson版本.. 但什麼發生在Debian的服務器是simplejson是增加額外的精密座標值 -Python 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版本,但沒有成功。

+1

嗯不知道這一點。這可能會有幫助,但它不是simplejson:http://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module – valheru

+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] 
+0

完美..謝謝..這對我有用..棘手的部分是在Debian 6上安裝Python 2.7,但該問題已通過http://www.yodi.sg/install-python-2-7-debian- 6-0-squeeze/ – UberNeo

+0

順便說一句,使這項工作在Python 2.6中的任何解決方法,試圖使用這個http://stackoverflow.com/a/1447562/2374316 ..但這似乎也不能在2.6 – UberNeo

+0

你真的需要改變這個?正如Robert Siemer在他的回答中指出的那樣,這實際上只是一種視覺差異。 2.6或2.7表示的二進制值應該相同。否則,升級到Python 2.7可能會更簡單。 –

0

@斯內德的答案是正確的,但我想補充:

您的問題只是視覺一個,雙方表示是完全是一個「正常」的計算機上的相同二者也並不完全是什麼電腦(罐)店

>>> 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,它是「short」或「legacy」)。