在 Python 中打印浮点值(Mapbox 地理坐标)
Printing float values in Python (Mapbox geo-coordinates)
我正在抓取一个网站,以便使用他们的 API.
通过 MapBox 地理编码器获取我当时 运行 的地址
基本代码为:
geocoder = mapbox.Geocoder(access_token='my_token')
response = geocoder.forward(address)
result = response.json()
我在抓取网页时即时输入地址。
当我打印 result
时,我的地理代码输出是:
{u'attribution': u'NOTICE: \xa9 2019 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare.', u'query': [u'1151', u'folsom', u'st', u'san', u'francisco', u'ca', u'94103'], u'type': u'FeatureCollection', u'features': [{u'center': [-122.408767, 37.775679], u'geometry': {u'type': u'Point', u'coordinates': [-122.408767, 37.775679]}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'point'}, u'context': [{u'text': u'South of Market', u'id': u'neighborhood.293577'}, {u'text': u'94103', u'id': u'postcode.13892342768265050'}, {u'text': u'San Francisco', u'wikidata': u'Q62', u'id': u'place.15734669613361910'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 1, u'type': u'Feature', u'id': u'address.2415498141648290', u'place_name': u'1151 Folsom Street, San Francisco, California 94103, United States'}, {u'center': [-122.41, 37.78], u'geometry': {u'type': u'Point', u'coordinates': [-122.41, 37.78]}, u'text': u'94103', u'place_type': [u'postcode'], u'properties': {}, u'bbox': [-122.42770549164, 37.7635596779962, -122.397810189758, 37.7880518462807], u'context': [{u'text': u'San Francisco', u'wikidata': u'Q62', u'id': u'place.15734669613361910'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'relevance': 0.75, u'type': u'Feature', u'id': u'postcode.13892342768265050', u'place_name': u'San Francisco, California 94103, United States'}, {u'center': [-120.351259, 36.149281], u'geometry': {u'type': u'Point', u'coordinates': [-120.351259, 36.149281], u'interpolated': True}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'interpolated'}, u'context': [{u'text': u'93210', u'id': u'postcode.15655570180150430'}, {u'text': u'Coalinga', u'wikidata': u'Q985263', u'id': u'place.11616473478769790'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 0.4, u'type': u'Feature', u'id': u'address.165808680526830', u'place_name': u'1151 Folsom Street, Coalinga, California 93210, United States'}, {u'center': [-121.270558, 37.935765], u'geometry': {u'omitted': True, u'type': u'Point', u'coordinates': [-121.270558, 37.935765], u'interpolated': True}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'interpolated'}, u'context': [{u'text': u'Stockton Metropolitan Airport', u'id': u'neighborhood.289380'}, {u'text': u'95206', u'id': u'postcode.5171535940442000'}, {u'text': u'Stockton', u'wikidata': u'Q49240', u'id': u'place.8554008982780520'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 0.4, u'type': u'Feature', u'id': u'address.2752377031224260', u'place_name': u'1151 Folsom Street, Stockton, California 95206, United States'}, {u'center': [-121.3078506, 38.5879294], u'geometry': {u'type': u'Point', u'coordinates': [-121.3078506, 38.5879294]}, u'text': u'Folsom Boulevard', u'place_type': [u'address'], u'properties': {u'accuracy': u'street'}, u'context': [{u'text': u'Cordova Gardens', u'id': u'neighborhood.289486'}, {u'text': u'95670', u'id': u'postcode.7919884525467840'}, {u'text': u'Rancho Cordova', u'wikidata': u'Q986928', u'id': u'place.5058833534899630'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'relevance': 0.398, u'type': u'Feature', u'id': u'address.8435179963733262', u'place_name': u'Folsom Boulevard, Rancho Cordova, California 95670, United States'}]}
然后我得到输出的坐标,使用:
coordinates = result['features'][1]['center']
但是,当我尝试为同一输出打印 coordinates
(地理坐标)时,我得到:
[-122.41, 37.78]
如何打印坐标的完整浮点数?目前,它似乎将数字四舍五入到小数点后两位。我试过使用: print ["%0.6f" % i for i in coordinates]
但这只是在它的末尾添加了零。谢谢。
这就是那些坐标的完整浮点数。
如果您检查 json,您会发现 只有 第二个 center
(在索引 1 处)有两位小数。其余的至少有六位小数。
运行:
for coordinates in result["features"]:
print(coordinates["center"])
结果:
[-122.408767, 37.775679]
[-122.41, 37.78]
[-120.351259, 36.149281]
[-121.270558, 37.935765]
[-121.3078506, 38.5879294]
我正在抓取一个网站,以便使用他们的 API.
通过 MapBox 地理编码器获取我当时 运行 的地址基本代码为:
geocoder = mapbox.Geocoder(access_token='my_token')
response = geocoder.forward(address)
result = response.json()
我在抓取网页时即时输入地址。
当我打印 result
时,我的地理代码输出是:
{u'attribution': u'NOTICE: \xa9 2019 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare.', u'query': [u'1151', u'folsom', u'st', u'san', u'francisco', u'ca', u'94103'], u'type': u'FeatureCollection', u'features': [{u'center': [-122.408767, 37.775679], u'geometry': {u'type': u'Point', u'coordinates': [-122.408767, 37.775679]}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'point'}, u'context': [{u'text': u'South of Market', u'id': u'neighborhood.293577'}, {u'text': u'94103', u'id': u'postcode.13892342768265050'}, {u'text': u'San Francisco', u'wikidata': u'Q62', u'id': u'place.15734669613361910'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 1, u'type': u'Feature', u'id': u'address.2415498141648290', u'place_name': u'1151 Folsom Street, San Francisco, California 94103, United States'}, {u'center': [-122.41, 37.78], u'geometry': {u'type': u'Point', u'coordinates': [-122.41, 37.78]}, u'text': u'94103', u'place_type': [u'postcode'], u'properties': {}, u'bbox': [-122.42770549164, 37.7635596779962, -122.397810189758, 37.7880518462807], u'context': [{u'text': u'San Francisco', u'wikidata': u'Q62', u'id': u'place.15734669613361910'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'relevance': 0.75, u'type': u'Feature', u'id': u'postcode.13892342768265050', u'place_name': u'San Francisco, California 94103, United States'}, {u'center': [-120.351259, 36.149281], u'geometry': {u'type': u'Point', u'coordinates': [-120.351259, 36.149281], u'interpolated': True}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'interpolated'}, u'context': [{u'text': u'93210', u'id': u'postcode.15655570180150430'}, {u'text': u'Coalinga', u'wikidata': u'Q985263', u'id': u'place.11616473478769790'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 0.4, u'type': u'Feature', u'id': u'address.165808680526830', u'place_name': u'1151 Folsom Street, Coalinga, California 93210, United States'}, {u'center': [-121.270558, 37.935765], u'geometry': {u'omitted': True, u'type': u'Point', u'coordinates': [-121.270558, 37.935765], u'interpolated': True}, u'text': u'Folsom Street', u'place_type': [u'address'], u'properties': {u'accuracy': u'interpolated'}, u'context': [{u'text': u'Stockton Metropolitan Airport', u'id': u'neighborhood.289380'}, {u'text': u'95206', u'id': u'postcode.5171535940442000'}, {u'text': u'Stockton', u'wikidata': u'Q49240', u'id': u'place.8554008982780520'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'address': u'1151', u'relevance': 0.4, u'type': u'Feature', u'id': u'address.2752377031224260', u'place_name': u'1151 Folsom Street, Stockton, California 95206, United States'}, {u'center': [-121.3078506, 38.5879294], u'geometry': {u'type': u'Point', u'coordinates': [-121.3078506, 38.5879294]}, u'text': u'Folsom Boulevard', u'place_type': [u'address'], u'properties': {u'accuracy': u'street'}, u'context': [{u'text': u'Cordova Gardens', u'id': u'neighborhood.289486'}, {u'text': u'95670', u'id': u'postcode.7919884525467840'}, {u'text': u'Rancho Cordova', u'wikidata': u'Q986928', u'id': u'place.5058833534899630'}, {u'short_code': u'US-CA', u'text': u'California', u'wikidata': u'Q99', u'id': u'region.11319063928738010'}, {u'short_code': u'us', u'text': u'United States', u'wikidata': u'Q30', u'id': u'country.9053006287256050'}], u'relevance': 0.398, u'type': u'Feature', u'id': u'address.8435179963733262', u'place_name': u'Folsom Boulevard, Rancho Cordova, California 95670, United States'}]}
然后我得到输出的坐标,使用:
coordinates = result['features'][1]['center']
但是,当我尝试为同一输出打印 coordinates
(地理坐标)时,我得到:
[-122.41, 37.78]
如何打印坐标的完整浮点数?目前,它似乎将数字四舍五入到小数点后两位。我试过使用: print ["%0.6f" % i for i in coordinates]
但这只是在它的末尾添加了零。谢谢。
这就是那些坐标的完整浮点数。
如果您检查 json,您会发现 只有 第二个 center
(在索引 1 处)有两位小数。其余的至少有六位小数。
运行:
for coordinates in result["features"]:
print(coordinates["center"])
结果:
[-122.408767, 37.775679]
[-122.41, 37.78]
[-120.351259, 36.149281]
[-121.270558, 37.935765]
[-121.3078506, 38.5879294]