LoginSignup
0
2

More than 5 years have passed since last update.

KEN_ALL.CSV から郵便番号データを手軽に作る

Posted at

やっかいな KEN_ALL.CSVと JIGYOSYO.CSV を使って郵便番号データを手軽に作る方法です。

ken-all コマンドのインストール

郵便番号データを加工するコマンドをインストール

$ go get github.com/inouet/ken-all

(go が入ってない場合は、brew install goしておく)

データ生成

下記コマンドの実行すると、KEN_ALL.CSV とJIGYOSYO.CSV がダウンロードされ、1つの郵便番号データが生成されます。

$ cd $GOPATH/src/github.com/inouet/ken-all

$ ./script/make-zip -o /tmp/ken-all.tsv

# 確認

$ head -n 2 /tmp/ken-all.tsv
0600000 1   北海道   札幌市中央区              10
0640941 1   北海道   札幌市中央区  旭ケ丘           10

テーブル作成

mysqlの場合

$ mysql -u user -p db_name < example/zip_code.mysql.sql

sqliteの場合

$ sqlite3 zip.db <  example/zip_code.sqlite.sql

データのインポート

mysqlの場合

mysql> load data infile '/tmp/ken-all.tsv' into table zip_code;

sqliteの場合

$ sqlite3 zip.db
sqlite> .separator "\t"
sqlite> .import /tmp/ken-all.tsv zip_code

確認

mysqlの場合

mysql> select * from zip_code where zip_code in ('1066118', '1008792');
+----------+----------+-----------+--------------+----------------------------------------------+--------+---------------------------+-------------+
| zip_code | zip_type | pref      | city         | town                                         | street | name                      | update_code |
+----------+----------+-----------+--------------+----------------------------------------------+--------+---------------------------+-------------+
| 1066118  |        1 | 東京都    | 港区         | 六本木六本木ヒルズ森タワー18階               |        |                           |          10 |
| 1008792  |        2 | 東京都    | 千代田区     | 大手町                                       | 2-3-1  | 日本郵便 株式会社         |          20 |
+----------+----------+-----------+--------------+----------------------------------------------+--------+---------------------------+-------------+

sqliteの場合

sqlite> .mode column
sqlite> select * from zip_code where zip_code in ('1066118', '1008792');

zip_code    zip_type    pref        city        town              street      name        update_code
----------  ----------  ----------  ----------  ----------------  ----------  ----------  -----------
1066118     1           東京都         港区          六本木六本木ヒルズ森タワー18階                          10
1008792     2           東京都         千代田区        大手町               2-3-1       日本郵便 株式会社   20

おしまい。

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