Печать значений с плавающей точкой в ​​Python (гео-координаты Mapbox) - PullRequest
1 голос
/ 06 мая 2019

Я очищаю веб-сайт, чтобы получить адрес, который затем запускаю через геокодер MapBox, используя их API.

Основной код:

geocoder = mapbox.Geocoder(access_token='my_token')


response = geocoder.forward(address)
result = response.json()

IКормлю по адресам на лету, а я соскребаю веб-страницу.

Когда я печатаю 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]

Как я могу получить это, чтобы напечатать полный float координат?В настоящее время кажется, что числа округляются до двух десятичных знаков.Я попытался использовать: print ["%0.6f" % i for i in coordinates], но это просто добавляет нули в конце.Благодаря.

1 Ответ

1 голос
/ 06 мая 2019

Это полное число этих координат.

Если вы изучите 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]
...