LoginSignup
2
1

More than 3 years have passed since last update.

都道府県名を都道府県コードに変換

Posted at

都道府県名を次で定義されている都道府県コードに変換する方法です。
都道府県コード

変換に使うJSONファイル

prefecture.json
{
  "北海道": 1,
  "青森県": 2,
  "岩手県": 3,
  "宮城県": 4,
  "秋田県": 5,
  "山形県": 6,
  "福島県": 7,
  "茨城県": 8,
  "栃木県": 9,
  "群馬県": 10,
  "埼玉県": 11,
  "千葉県": 12,
  "東京都": 13,
  "神奈川県": 14,
  "新潟県": 15,
  "富山県": 16,
  "石川県": 17,
  "福井県": 18,
  "山梨県": 19,
  "長野県": 20,
  "岐阜県": 21,
  "静岡県": 22,
  "愛知県": 23,
  "三重県": 24,
  "滋賀県": 25,
  "京都府": 26,
  "大阪府": 27,
  "兵庫県": 28,
  "奈良県": 29,
  "和歌山県": 30,
  "鳥取県": 31,
  "島根県": 32,
  "岡山県": 33,
  "広島県": 34,
  "山口県": 35,
  "徳島県": 36,
  "香川県": 37,
  "愛媛県": 38,
  "高知県": 39,
  "福岡県": 40,
  "佐賀県": 41,
  "長崎県": 42,
  "熊本県": 43,
  "大分県": 44,
  "宮崎県": 45,
  "鹿児島県": 46,
  "沖縄県": 47
}

bash での使用例

test_bash.sh
#
#
for name in {"北海道","宮城県","栃木県","静岡県","広島県"}
do
    echo -n $name"  "
    jq .\"$name\" prefecture.json
done
#

実行結果

$ ./test_bash.sh 
北海道   1
宮城県   4
栃木県   9
静岡県   22
広島県   34

Python3 での使用例

test_python.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   test_python.py
#
#                   Apr/29/2020
#
# ------------------------------------------------------------------
import  sys
import  os
import  json
#
#
# ------------------------------------------------------------------
def file_to_str_proc(file_in):
    str_out = ""
    try:
        fp_in = open(file_in,encoding='utf-8')
        str_out = fp_in.read()
        fp_in.close()
    except Exception as ee:
        sys.stderr.write("*** error *** file_to_str_proc ***\n")
        sys.stderr.write(str (ee))
#
    return  str_out
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
prefecture_json = sys.argv[1]
#
sys.stderr.write(prefecture_json + "\n")
#
json_str = file_to_str_proc(prefecture_json)
dict_pref = json.loads(json_str)
#
name_aa = ["北海道","宮城県","栃木県","静岡県","広島県"]
for name in name_aa:
    code = dict_pref[name]
    print(name,code)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./test_python.py prefecture.json
*** 開始 ***
prefecture.json
北海道 1
宮城県 4
栃木県 9
静岡県 22
広島県 34
*** 終了 ***

node.js での使用例

test_node.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  test_node.js
//
//                  Apr/29/2020
//
// ---------------------------------------------------------------
var fs = require("fs")

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const prefecture_json=process.argv[2]

console.log (prefecture_json)

const json_str = fs.readFileSync (prefecture_json,'utf8')

const dict_pref = JSON.parse (json_str)

name_aa = ["北海道","宮城県","栃木県","静岡県","広島県"]
for(var it in name_aa)
    {
    const name = name_aa[it]
    const code = dict_pref[name]
    console.log(name,code)
    }

console.error ("*** 終了 ***")
// ---------------------------------------------------------------

実行結果

$ ./test_node.js prefecture.json
*** 開始 ***
prefecture.json
北海道 1
宮城県 4
栃木県 9
静岡県 22
広島県 34
*** 終了 ***

Go の使用例

test_go.go
// ---------------------------------------------------------------
//
//  test_go.go
//
//                  Apr/29/2020
// ---------------------------------------------------------------
package main 

import (
    "os"
    "fmt"
    "encoding/json"
    "io/ioutil"
)

// ---------------------------------------------------------------
func main () {
    fmt.Fprintf (os.Stderr,"*** 開始 ***\n")

    file_json := os.Args[1]
    fmt.Printf ("%s\n",file_json)

    buff,_ := ioutil.ReadFile (file_json)

    json_str := string(buff)

    var dict_pref map[string](int)

    json.Unmarshal ([]byte(json_str), &dict_pref )

    name_aa := [5]string{"北海道","宮城県","栃木県","静岡県","広島県"}

    for _,name := range name_aa {
        code := dict_pref[name]
        fmt.Printf("%s\t%d\n", name,code)
        }

    fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------

実行結果

$ go run test_go.go prefecture.json
*** 開始 ***
prefecture.json
北海道   1
宮城県   4
栃木県   9
静岡県   22
広島県   34
*** 終了 ***
2
1
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
2
1