Prefectures
import re
#Prefectures only
address = "1 Nishi-Shinjuku, Shinjuku-ku, Tokyo-1-1"
matches = re.match('Tokyo|Hokkaido|(?:Kyoto|Osaka)Fu|.{2,3}Prefecture' , address)
print(matches.group())
#--->Tokyo
Ward
import re
#Ward only
address = "1 Nishi-Shinjuku, Shinjuku-ku, Tokyo-1-1"
ku_number = address.find('Ward')
print(address[:ku_number+1]) #If not, blanks are returned
#--->Shinjuku-ku, Tokyo
city
import re
#City only
address = "1 Yokoyama-cho, Hachioji-shi, Tokyo-1"
shi_number = address.find('city')
print(address[:shi_number+1]) #If not, blanks are returned#If not, blanks are returned
#--->Hachioji, Tokyo
Towns and villages can also be obtained with .find ().
Recommended Posts