python - 在 Python 中打印浮点值(Mapbox 地理坐标)
问题描述
我正在抓取一个网站以获取地址,然后我使用他们的 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]
推荐阅读
- reactjs - 测试 React 功能组件承诺使用 Jest 的行为,
- java - 如何在 Spring Boot 项目中设置日志的颜色
- javascript - React JS - 博客和博客详细页面 URL 步骤
- amazon-s3 - 使用 Codepipeline/Codebuild/S3 实现以下目标的最佳方法是什么
- algorithm - 图像量化与量子算法问题
- django - Django:使用 SQLITE 实现带有可选分隔符的 Group_Concat
- java - JAVA/ANDROID - 在 url.endsWith(".html")) WebView 时打开 SecondActivity
- popup - 如何使此视频弹出窗口居中
- vba - 项目 VBA File_Subs 未显示
- python - Webhooks Python 树莓派