Last active
April 12, 2020 15:28
-
-
Save kebhr/20179628e34d9912a1e7396a87c9dd9b to your computer and use it in GitHub Desktop.
日本郵便のCSVデータと code4fukui/localgovjp から 都道府県名(JP/EN)・市町村名(JP/EN)・ホームページURL のCSVを生成するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"flag" | |
"fmt" | |
"golang.org/x/text/encoding/japanese" | |
"golang.org/x/text/transform" | |
"io" | |
"log" | |
"os" | |
"strings" | |
) | |
type Municipality struct { | |
Pref string | |
PrefEn string | |
Name string | |
NameEn string | |
URL string | |
} | |
func main() { | |
flag.Parse() | |
file, err := os.Open(flag.Arg(0)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
file2, err := os.Open(flag.Arg(1)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file2.Close() | |
output, err := os.Create(flag.Arg(2)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer output.Close() | |
reader := csv.NewReader(transform.NewReader(file, japanese.ShiftJIS.NewDecoder())) | |
reader.LazyQuotes = true | |
readerForURL := csv.NewReader(transform.NewReader(file2, japanese.ShiftJIS.NewDecoder())) | |
readerForURL.LazyQuotes = true | |
writer := csv.NewWriter(transform.NewWriter(output, japanese.ShiftJIS.NewEncoder())) | |
writer.UseCRLF = true | |
cityToURL := make(map[string]string) | |
for { | |
rawRecord, err := readerForURL.Read() | |
if err == io.EOF { | |
break | |
} else if err != nil { | |
log.Fatal(err) | |
} | |
var record []string | |
for i, v := range rawRecord { | |
if i > 0 { | |
record = append(record, v) | |
} | |
} | |
key := record[0] + record[2] | |
cityToURL[key] = record[6] | |
} | |
var municipalities []Municipality | |
for { | |
rawRecord, err := reader.Read() | |
if err == io.EOF { | |
break | |
} else if err != nil { | |
log.Fatal(err) | |
} | |
var record []string | |
for i, v := range rawRecord { | |
if i > 0 { | |
record = append(record, v) | |
} | |
} | |
if strings.Contains(record[1], " ") { | |
s := strings.Split(record[1], string(' ')) | |
if strings.Contains(record[1], "区") { | |
record[1] = s[len(s)-2] | |
} else { | |
record[1] = s[len(s)-1] | |
} | |
} | |
if strings.Contains(record[4], " ") { | |
s := strings.Split(record[4], string(' ')) | |
if len(s) == 2 { | |
// 〜市/町/村/区(東京都) | |
record[4] = strings.Title(strings.ToLower(s[0])) + strings.ToLower("-" + s[1]) | |
} else if len(s) == 4 { | |
// 〜郡 〜市/町/村 or 〜市/町/村 〜区 | |
if s[1] == "GUN" { | |
record[4] = strings.Title(strings.ToLower(s[2])) + strings.ToLower("-" + s[3]) | |
} else { | |
record[4] = strings.Title(strings.ToLower(s[0])) + strings.ToLower("-" + s[1]) | |
} | |
} else { | |
// 例外パターン | |
if record[4] == "MIYAKEJIMA MIYAKE MURA" { | |
record[4] = "Miyake-mura" | |
} else if record[4] == "HACHIJOJIMA HACHIJO MACHI" { | |
record[4] = "Hachijo-machi" | |
} else if record[4] == "NISHIYATSUSHIRO GUN ICHIKAWAMISATO" { | |
record[4] = "Ichikawamisato-cho" | |
} | |
} | |
} | |
if record[3] == "HOKKAIDO" { | |
record[3] = "Hokkaido" | |
} else { | |
s := strings.Split(record[3], " ") | |
record[3] = strings.Title(strings.ToLower(s[0])) | |
// record[3] = strings.Title(strings.ToLower(s[0])) + "-" + strings.ToLower(s[1]) | |
} | |
dup := false | |
for _, munic := range municipalities { | |
if munic.Pref == record[0] && munic.Name == record[1] { | |
dup = true | |
break | |
} | |
} | |
if !dup { | |
municipalities = append(municipalities, Municipality{ | |
Pref: record[0], | |
PrefEn: record[3], | |
Name: record[1], | |
NameEn: record[4], | |
URL: cityToURL[record[0] + record[1]], | |
}) | |
if err := writer.Write([]string{record[0], record[3], record[1], record[4], cityToURL[record[0] + record[1]]}); err != nil { | |
fmt.Println(err) | |
} | |
} | |
} | |
// | |
// for i, v := range municipalities { | |
// fmt.Printf("%d: 県: %s 市: %s 県EN: %s 市EN: %s URL:%s\n", i, v.Pref, v.Name, v.PrefEn, v.NameEn, v.URL) | |
// } | |
writer.Flush() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment