【发布时间】:2019-02-03 20:45:39
【问题描述】:
我目前正在使用 Python 3.5 下的 geopy 包。我使用 geopy.Point 包。我将它包含在很多文件中,从未遇到过问题。不幸的是,我现在在调试时遇到了麻烦,因为 Point __str__ 函数默认使用大地基准系统作为输出。
如果可能的话,我宁愿只看到纬度和经度。我不知道geopy为什么使用这个大地测量系统,所以可能是我没有得到它的优势。但据我所知,这个系统对调试没有多大帮助。
所以现在我寻求一种以非常优雅的方式更改__str__ 函数的方法。最pythonic的方法是什么?据我所知,仅仅编写一个包装器就会导致代码完成。
这里有一个例子来解释我的意思:
from geopy.point import * #import the package
def str_p(point): #a function to get the format I actually can read
return "Lat= "+str(point.latitude)+" | "+"Long= "+str(point.longitude) #always writing .latitude or .longitude when debugging is troublesome
p = Point(55, 17) #create a Point, note that we are using Lat, Lng
print(p) # 55 0m 0s N, 17 0m 0s E , a good format if you are manning a pirate ship. But confusing to me
print(str_p(p))# what I want
【问题讨论】:
-
geopy 维护者在这里。您不必是海盗就可以真正需要地理位置格式。我相信你正在搜索的是
repr(Point(55, 17)),它会给你'Point(55.0, 17.0, 0.0)'。
标签: python python-3.x geopy