天天看点

Python: 百度API实现火星坐标转百度坐标

以下是我写的调用百度API将火星坐标转换成百度坐标的python代码:

# -*- coding: utf-8 -*-

import sys

reload(sys)

sys.setdefaultencoding("utf-8")

from urlparse import urlparse

from urlparse import urlunparse

from urlparse import urljoin

import urllib

import os

api="http://api.map.baidu.com/geoconv/v1/?coords="

f=open("coortest_20.txt")

tof=open("liru.txt",'w')

while True:

 line=f.readline().strip('\n')

 if line:

  try:

   jwlist=line.split(',')

   jingdu=str(jwlist[1])

   weidu=str(jwlist[0])

   url=api+jingdu+","+weidu+"&from=1&to=5&ak=秘钥"

   res=urllib.urlopen(url)

   buffer=res.read()

   split_buffer=buffer.split('"x":')

   t=split_buffer[1]

   t_split=t.split(',"y":')

   x=t_split[0]

   y_split=t_split[1].split('}]}')

   y=y_split[0]

   print x,y

   #print buffer[]

   tof.write(x)

   tof.write(y)

   tof.write('\n')

  except:

   tof.write("ERROR")

   tof.write('\n')

 else:

  break

tof.close()

print "finished!"