Interactive console application for address and zip code search

Introduction

This is the first post of Advent Calendar. Since the python frame was empty, I will post one case while I am afraid.

Click here for the previous article → How to get weather information with LINE Notify + Python

background

It's early December this year. December is Christmas, but it's also the time to prepare a New Year's card.

When I thought about issuing a New Year's card, I knew the address, but what was the zip code ...? Do you ever think that? (And vice versa)

Now, if you do a google search for the address, you will get the zip code, I wish there was a tool that could search for addresses and zip codes without an internet environment. After mastering python, I made a simple console application.

There are quite a few cases of searching for an address by zip code, I thought that there were not many cases of searching for a zip code from an address, so I tried to handle it in either case.

environment

· Mac OS Mojave ・ Python3 ・ Pyinstaller

Processing specifications

The flowchart is as follows. Basically, do you want to dump the address of the zip code you entered? The format is such that the user can interactively select whether to dump the zip code corresponding to the entered address (only some place names are acceptable).

FindAddress.png

Source code

I download "KEN_ALL.CSV" from the postal code data download page from the post office web page, and put it directly under / Users / to load it.

https://www.post.japanpost.jp/zipcode/download.html

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()

Make exe with pyinstaller

Please refer to this article for the procedure of exe conversion because it is easy to understand.

Make an exe file with PyInstaller https://qiita.com/takanorimutoh/items/53bf44d6d5b37190e7d1

Execution result

As an example, let's search for the address and zip code of Shinkiba in Koto-ku, Tokyo.

If you enter the postal code "1360082" in the address search of 1, the address of Shinkiba "Shinkiba, Koto-ku, Tokyo" will be dumped. If you enter the address keyword "Shinkiba" in the postal code search of 2, the postal codes and addresses of all the addresses containing the characters of Shinkiba will be dumped as a set.

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

important point

Before using it, please set the setting at the end of the shell to "Close window" in the terminal environment settings. If you do not do this, the console will remain after the app is closed.

名称未設定.png

Finally

In the future, if I have time, I will try to implement a version compatible with Windows and Linux.

Recommended Posts

Interactive console application for address and zip code search
Interactive console application for address and zip code search
Comfortable LaTeX with Windows Subsystem for Linux and VS Code
Get the address from the zip code
Vectorize sentences and search for similar sentences
Causal reasoning and causal search with Python (for beginners)