2021年11月18日 星期四

[程式碼] Python 切割臺灣地址 程式碼

Python 切割臺灣地址
Python 程式碼 如下
#切割地址的函式
def address(address):
  swap = address
  address1 = '' #縣市
  address2 = '' #區鄉鎮市
  address3 = '' #村里
  address4 = '' #鄰
  address5 = '' #路段
  address6 = '' #巷
  address7 = '' #弄
  address8 = '' #號
  address9 = '' #樓
  address10 = '' #其他備註

  print('原本地址:'+swap)

  #縣市

  if '市' in address :
    index = address.find('市')+1  
    address1 = address[:index]
    swap = address[index:] 
  if '縣' in address :
    index = address.find('縣')+1  
    address1 = address[:index]
    swap = address[index:] 

  #區鄉鎮市

  if '區' in swap :
    index = swap.find('區')+1
    address2 = swap[:index]  
    swap = swap[index:]
  if '鄉' in swap :
    index = swap.find('鄉')+1
    address2 = swap[:index]  
    swap = swap[index:]
  if '鎮' in swap :
    index = swap.find('鎮')+1
    address2 = swap[:index]  
    swap = swap[index:]
  if '市' in swap :
    index = swap.find('市')+1
    address2 = swap[:index]  
    swap = swap[index:]

  #村里鄰

  if '村' in swap :
    index = swap.find('村')+1
    address3 = swap[:index]  
    swap = swap[index:]
  if '里' in swap :
    index = swap.find('里')+1
    address3 = swap[:index]  
    swap = swap[index:]
  if '鄰' in swap :
    index = swap.find('鄰')+1
    address4 = swap[:index]  
    swap = swap[index:]



  #要考慮路段問題
  if '段' in swap :
    index = swap.find('段')+1
    address5 = swap[:index]  
    swap = swap[index:]
  if '路' in swap :
    index = swap.find('路')+1
    address5 = swap[:index]  
    swap = swap[index:]

  if '巷' in swap :
    index = swap.find('巷')+1
    address6 = swap[:index]   
    swap = swap[index:]
  if '弄' in swap :
    index = swap.find('弄')+1 
    address7 = swap[:index]  
    swap = swap[index:]
  if '號' in swap :
    index = swap.find('號')+1 
    address8 = swap[:index]  
    swap = swap[index:]
  if '樓' in swap :
    index = swap.find('樓')+1 
    address9 = swap[:index]  
    swap = swap[index:]
  address10 = swap

  print('縣市:'+address1) #測設用程式碼
  print('鄉鎮市區:'+address2) #測設用程式碼
  print('村里:'+address3) #測設用程式碼
  print('鄰:'+address4) #測設用程式碼
  print('路段:'+address5) #測設用程式碼
  print('巷:'+address6) #測設用程式碼
  print('弄:'+address7) #測設用程式碼
  print('號:'+address8) #測設用程式碼
  print('樓:'+address9) #測設用程式碼
  print('其他:'+address10) #測設用程式碼
  print('---------------------------------')

#主程式
address('臺北市中正區延平南路11號')
address('嘉義縣太保市太保里66號')
address('臺北市中正區重慶南路1段122號')
address('桃園市龜山區大崗里樹人路56號(位置簡圖)')