在本教程中,我们将讨论如何使用Python中的geopy模块来获取特定位置的邮政编码。geopy使Python用户能够在全球范围内查找地址、城市和国家的坐标。

要安装Geopy模块,用户可以运行以下命令:

pip3 install geopy  

如何获取邮政编码

我们将按照以下步骤进行操作:

  • 步骤 1: 导入geopy模块
  • 步骤 2: 使用Nominatim API初始化以获取输入字符串的位置。
  • 步骤 3: 我们将使用geo_locator.geocode()函数来获取位置。
  • 步骤 4: 从给定的列表中获取信息,并使用raw函数将其解析为字典。
  • 步骤 5: 从位置实例中提取邮政编码数据。

步骤 1: 导入geopy模块

from geopy.geocoders import Nominatim

步骤 2: 创建Nominatim对象并使用geoapiExercises参数初始化Nominatim API。

geo_locator = Nominatim(user_agent = "geoapiExercises")  

步骤 3: 使用geocode()函数获取完整地址

place_1 = "RidgePoint Irving, Texas, USA"  
location = geo_locator.geocode(place_1)  
print(location)  

输出:

Ridgepoint Drive, Irving, Dallas County, Texas, 75063, United States

步骤 4: 从给定列表中获取信息并使用raw解析为字典。

data_1 = location.raw  
print (data_1)  

输出:

{' place_id ': 209975995, ' licence ': ' Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright ', ' osm_type ': ' way ', ' osm_id ': 567473012, ' boundingbox ': [' 32.908872 ', ' 32.9091407 ', ' -96.9887504 ', ' -96.9883353 '], ' lat ': ' 32.9088978 ', ' lon ': ' -96.9886835 ', ' display_name ':       ' Ridgepoint Drive, Irving, Dallas County, Texas, 75063, United States ', ' class ': 'highway ', ' type ': ' tertiary ', ' importance ': 0.42000000000000004}

步骤 5: 从位置实例中提取邮政编码数据。

location_data = data_1['display_name'].split()  
print ("The Full Location is: ")  
print (location_data)  
print ("The Zip code of the location is: ", location_data[-3])  

输出:

The Full Location is: 
['Ridgepoint', 'Drive,', 'Irving,', 'Dallas', 'County,', 'Texas,', '75063,', 'United', 'States']
The Zip code of the location is:  75063,

完整的Python代码示例

示例 -

from geopy.geocoders import Nominatim  
geo_locator = Nominatim(user_agent = "geoapiExercises")  
place_1 = "Disneyland Dr, Anaheim, USA"  
location = geo_locator.geocode(place_1)  
print(location)  
data_1 = location.raw  
print (data_1)  
location_data = data_1['display_name'].split()  
print ("\nThe Full Location is: ")  
print (location_data)  
print ("The Zip code of the location is: ", location_data[-3])  

输出:

Disneyland Drive, Anaheim, Orange County, California, 92812-9998, United States
{' place_id ': 211571693, ' licence ': ' Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright ', ' osm_type ': ' way ', ' osm_id ': 568143583, ' boundingbox ': [' 33.8184966 ', ' 33.8200236 ', ' -117.9229381 ', ' -117.9224363 '], ' lat ': ' 33.819073 ', ' lon ': ' -117.9226168 ', ' display_name ': 'Disneyland Drive, Anaheim, Orange County, California, 92812-9998, United States ', ' class ': ' highway ', ' type ': ' secondary ', ' importance ': 0.41000000000000003}

The Full Location is: 
['Disneyland', 'Drive,', 'Anaheim,', 'Orange', 'County,', 'California,', '92812-9998,', 'United', 'States']
The Zip code of the location is:  92812-9998,

结论

在本教程中,我们讨论了用户如何使用Python中的geopy模块获取给定位置的邮政编码。我们还展示了示例以便更好理解。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程