郵便番号→住所変換スクリプト
あるバイトで必要になったため、作ってみた。ソース汚いし、効率考慮してないです…。
誰かアドバイスください。
尚、事前に以下のような郵便番号一覧ファイルを作っておく。
6061122 6069877 …
csvは郵便局のページからdownloadしてくる。
以下ソース。
#! /usr/bin/env ruby #郵便番号から住所を得るスクリプト #使いかた #jyu-shohenkan.rb 変換したいファイル 変換表(cvs) 変換後ファイル require "csv" $KCODE= "EUC" #csvから必要部分だけ残す def adjust(csv_line) csv_line=csv_line.split(/,/) csv_line.slice!(0,2) csv_line.slice!(1,3) csv_line.slice!(4,6) return csv_line end #出力用に整形 def address(csv_line) csv_line[1]=csv_line[1]+csv_line[2]+csv_line[3] csv_line.slice!(2,2) csv_line=csv_line.join(",") csv_line.delete!("\"") csv_line[3,0]="-" return csv_line end def main source=open(ARGV[0],"r") target=open(ARGV[2],"w") while line=source.gets line.delete!("\n") File.open(ARGV[1]){|csv| while line2=csv.gets line2=adjust(line2) #正規表現作成 reg=Regexp.new(line) if reg =~ line2[0] then line2=address(line2) target.puts line2 break end end } end source.close target.close end main