LoginSignup
0
0

GoogleMapの「住所をコピーする」で取得した住所から郵便番号を外すPython3コード(Pythonista3対応)

Posted at

はじめに

本コードはPython3およびiOS版アプリPythonista3を用いて、iPhone版GoogleMapAppの住所の部分をコピペしたものから、郵便番号を抜き取るものです。

  1. GoogleMapの住所部分をクリップボードにコピー
  2. iOSの場合はPythonista3を起動
  3. 本コードを実行
  4. クリップボードに「住所」のみコピー
  5. Pythonista3のコンソールに「郵便番号[改行]住所」を出力

という趣旨です。

用途

例えばiOSアプリ「乗換案内」の住所検索では郵便番号が入っている場合に、正常に検索してくれません。そのためGoogleMapアプリの目的地(あるいは出発地)から住所をコピペした後で、本コードを実行し、その後「乗換案内」にペーストして、住所タブを選択することで、郵便番号を削除する手間を省くといった利用方法があります。
(というか、そのためだけに作りました……)

前準備

Pythonista3は有料です。iOSでお使いの場合は別途お買い求めください。

ソースコード

google_address_V2.py
import sys

# クリップボードモジュール振り分け
try:
    import clipboard #Pythonista3用
    def copy_text_to_clipboard(text):
        clipboard.set(text)
    def paste_text_from_clipboard():
        return clipboard.get()
except ImportError:
    import pyperclip #Python3用
    def copy_text_to_clipboard(text):
        pyperclip.copy(text)
    def paste_text_from_clipboard():
        return pyperclip.paste()

# GoogleMap住所変換機能の本体
class ImportURL:  # クリップボードを取得するクラス
    def __init__(self):
        self.in_text = paste_text_from_clipboard()

class CaseAddress:  # 住所判定クラス
    def __init__(self, in_text):
        self.in_text = in_text
    
    def process(self):
        if '' in self.in_text[:2]:
            url = self.in_text[:9]
            text = self.in_text.replace(url, '')
            address = text.translate(str.maketrans({chr(0xFF01 + i): chr(0x21 + i) for i in range(94)}))
            address = address.replace('','-')
            address = address.lstrip()
            url = url.replace('','')
            copy_text_to_clipboard(address)  # クリップボードに住所を設定
            address = address.replace(' ','\n')
            print(url)
            print(address)

# 実行部分
if __name__ == '__main__':
    importURL = ImportURL()
    in_text = importURL.in_text  # クリップボードからテキストを取得
    caseAddress = CaseAddress(in_text)
    caseAddress.process()  # 住所処理を実行

終わりに

クリップボードに貼付後、変換後の文章がコンソールに表示されます。

0
0
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
0
0