LoginSignup
5
6

More than 3 years have passed since last update.

住所と郵便番号検索が可能な対話式コンソールアプリケーション

Last updated at Posted at 2019-12-04

はじめに

AdventCalendar初投稿です。
python枠が空いていたので、僭越ながら1件投稿させていただきます。

前回の記事はこちら→LINE Notify + Pythonで天気情報を取得する方法

背景

早いもので今年も12月。
12月といえばクリスマスですが、年賀状を用意する時期でもありますね。

年賀状を出そうと思ったとき、住所はわかるけど郵便番号何だったっけ・・・?と思ってしまうこと、ありませんか?(逆もまた然り)

今だと、住所をgoogle検索すれば郵便番号が出てきますが、
ネット環境が無くても住所・郵便番号が検索できるツールがあればと思い、
pythonの習熟がてら簡単なコンソールアプリを作ってみました。

郵便番号から住所を検索する事例は結構ありますが、
住所から郵便番号を検索する事例はあまりないと思ったので、どちらの場合でも対応できるようにしてみました。

環境

・Mac OS Mojave
・python3
・pyinstaller

処理仕様

フローチャートは下記の通りです。
基本的には、入力した郵便番号の住所をダンプするか
入力した住所(一部地名だけでも可)に対応する郵便番号をダンプするかをユーザに対話式で選んでもらう形式にしています。

FindAddress.png

ソースコード

郵便局のWebページから郵便番号データダウンロードページから"KEN_ALL.CSV"をダウンロードしておき、それを/Users/直下に置いて読み込むようにしています。

FindAddress_for_Mac.py
import sys

# Set Path including "KEN_ALL.CSV" for Mac OS X
csv_path = "/Users/KEN_ALL.CSV"

def GetAddress():
    postal_code = input("Type target postal code:")
    fp = open(csv_path, "r", encoding="shift_jis")
    for line in fp:
        line = line.replace(' ', '') # cut space
        line = line.replace('"', '') # cut double quatation
        cells = line.split(",")      # split by comma
        code = cells[2]  # Postal code
        pref = cells[6]  # Prefucture
        city = cells[7]  # City name
        ad = cells[8]    # Address name
        title = pref + city + ad
        if code.find(postal_code) != -1:
            print(title)
    fp.close()

def GetPostalCode():
    addr = input("Type target address:")
    fp = open(csv_path, "r", encoding="shift_jis")
    for line in fp:
        line = line.replace(' ', '') # cut space
        line = line.replace('"', '') # cut double quatation
        cells = line.split(",")      # split by comma
        code = cells[2]  # Postal Code
        pref = cells[6]  # Prefucture
        city = cells[7]  # City name
        ad = cells[8]    # Address name
        title = pref + city + ad
        if title.find(addr) != -1:
            print(code + ":" + title)
    fp.close()


def main():
    print("\n")
    print("####################################################")
    print("################### FINDADDRESS ####################")
    print("####################################################")
    print("\n")
    print("[DISCRIPTION]")
    print(" This tool is to get address or postal code.")
    print(" If you type '1', you can get unknown address by typing postal code.")
    print(" If you type '2', you can get unknown postal code by typing KEYWORD about address name.")
    print("\n")

    while True:
        print("[OPERATION]")
        print("input the following number which you want to get.")
        number = input("[1:Address  2:PostalCode  99 (or SPACE):Close]:")
        if number.isdigit() != 1: # Filter non-integer value
            sys.exit()
        int_number = int(number)  # Convert String to Integer
        if int_number == 1:       # The case that user types "1"
            GetAddress()
            print("\n")
        elif int_number == 2:     # The case that user types "2"
            GetPostalCode()
            print("\n")
        elif int_number == 99:    # The case that user types "99"
            sys.exit()
        else:                     # Exception handling
            print("Invalid Number. Type again.")
            print("\n")

if __name__ == "__main__":
    main()

pyinstallerでexe化

exe化の手順はこちらの記事がわかりやすいので参照。

PyInstallerでexeファイル化
https://qiita.com/takanorimutoh/items/53bf44d6d5b37190e7d1

実行結果

例として、東京都江東区の新木場の住所・郵便番号を検索してみます。

1の住所検索で郵便番号"1360082"を入力すれば、新木場の住所
"東京都江東区新木場"がダンプされます。
2の郵便番号検索で住所のキーワード"新木場"を入力すれば、新木場の文字が入っている住所すべての郵便番号と住所がセットでダンプされます。

スクリーンショット 2019-12-05 0.42.01.png

注意点

使用前にターミナルの環境設定でシェルの終了時の設定を「ウィンドウを閉じる」に設定するようにしてください。これをやらないとアプリ終了後にコンソールが残ります。

名称未設定.png

最後に

今後、時間があれば、Windows, Linux対応版の実装もしてみます。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6