关于Python中tsv文件解析的查询
我已经编写了一个python函数,它将使用GoogleAPI计算两点之间的距离。该功能如下图所示: 导入URL库 将json作为simplejson导入关于Python中tsv文件解析的查询,python,python-2.7,google-maps-api-3,Python,Python 2.7,Google Maps Api 3,我已经编写了一个python函数,它将使用GoogleAPI计算两点之间的距离。该功能如下图所示: 导入URL库 将json作为simplejson导入 def calculateDistance(sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude): sourceCoordinates = sourceLatitude, sourceLongitude destin
def calculateDistance(sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude):
sourceCoordinates = sourceLatitude, sourceLongitude
destinationCoordinates = destinationLatitude, destinationLongitude
url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(str(sourceCoordinates),str(destinationCoordinates))
result= simplejson.load(urllib.urlopen(url))
resulting_distance = result['rows'][0]['elements'][0]['distance']['text']
print "The distance is: " + resulting_distance
#calculateDistance(5.341612, -3.913602,5.342044, -4.143452)
#calculateDistance(5.342044, -4.143452,5.341612, -3.913602)
我有一个tsv文件,如下所示:
-4.143452 5.342044
-3.913602 5.341612
-3.967045 5.263331
....
....
第一列表示经度,第二列表示纬度
现在,我想将这些值的每个组合传递给我的函数,以获得点之间的距离。有人能告诉我一个有效的方法吗?tsv文件相当大。谢谢您的回复!您能告诉我如何将列表中的每个值组合传递给函数吗?您想将每一行逐个传递给函数还是一次传递全部?我想逐个传递每一行,并尝试所有可能的组合。
Points=[]
with open('file.tsv','r') as f:
for line in f:
Points.append(line.strip().split('\t'))
for combo in itertools.combinations(Points, 2):
calculateDistance(combo[0][0], combo[0][1], combo[1][0], combo[1][1])