33
34

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Go言語:SJISのCSVファイルを読み込む!

Last updated at Posted at 2013-12-18

この記事の目的

  • SJISのCSVを読み込めるようになる

まずはUTF-8のCSVを読み込んでみる

まず、UTF-8のCSVファイルを作る

  • 改行コード: CR LF

Macのエクセルで作ると改行コードがCRになるので注意

/tmp/chibashi.csv
ku,male population,female population
Chuoku,"202,213","102,080"
Hanamigawaku,"179,698","89,369"
Inageku,"156,612","78,199"
Wakabaku,"151,165","76,159"
Midoriku,"125,956","62,042"
Mihamaku,"148,951","72,806"

めんどくさい人は wget で落としてください

wget -O /tmp/chibashi.csv https://dl.dropboxusercontent.com/u/949822/qiita.com/131218-golang-read-csv/chibashi.csv

コードを書く

main.go
package main

import (
	"encoding/csv"
	"io"
	"log"
	"os"
)

func failOnError(err error) {
	if err != nil {
		log.Fatal("Error:", err)
	}
}

func main() {
	file, err := os.Open("/tmp/chibashi.csv")
	failOnError(err)
	defer file.Close()

	reader := csv.NewReader(file)

	for {
		record, err := reader.Read() // 1行読み出す
		if err == io.EOF {
			break
		} else {
			failOnError(err)
		}

		log.Printf("%#v", record)
	}
}

実行する

go run main.go

結果

2013/12/18 17:52:42 []string{"ku", "male population", "female population"}
2013/12/18 17:52:42 []string{"Chuoku", "202,213", "102,080"}
2013/12/18 17:52:42 []string{"Hanamigawaku", "179,698", "89,369"}
2013/12/18 17:52:42 []string{"Inageku", "156,612", "78,199"}
2013/12/18 17:52:42 []string{"Wakabaku", "151,165", "76,159"}
2013/12/18 17:52:42 []string{"Midoriku", "125,956", "62,042"}
2013/12/18 17:52:42 []string{"Mihamaku", "148,951", "72,806"}

SJISのCSVを読み込んでみる

  • 文字コード: SJIS
  • 改行コード: CR LF

Macのエクセルで作ると改行コードがCRになるので注意

/tmp/chibashi-sjis.csv
区,男性人口,女性人口,,,,,,
中央区,"202,213","102,080",,,,,,
花見川区,"179,698","89,369",,,,,,
稲毛区,"156,612","78,199",,,,,,
若葉区,"151,165","76,159",,,,,,
緑区,"125,956","62,042",,,,,,
美浜区,"148,951","72,806",,,,,,

めんどくさい人は wget で落としてください

wget -O /tmp/chibashi-sjis.csv https://dl.dropboxusercontent.com/u/949822/qiita.com/131218-golang-read-csv/chibashi-sjis.csv

SJISからUTF-8への変換はgithub.com/djimenez/iconv-go を使う。io.Readerのインターフェイスにそって作ってあるので、os.Fileとcsv.Readerとの間に挟んであげると、SJISをUTF-8に変換しながらCSVを読み込めるようになる。

github.com/djimenez/iconv-go をダウンロードする:

go get github.com/djimenez/iconv-go

コードを書く

main.go
package main

import (
	"encoding/csv"
	iconv "github.com/djimenez/iconv-go"
	"io"
	"log"
	"os"
)

func failOnError(err error) {
	if err != nil {
		log.Fatal("Error:", err)
	}
}

func main() {
	file, err := os.Open("/tmp/chibashi-sjis.csv")
	failOnError(err)
	defer file.Close()

	converter, err := iconv.NewReader(file, "sjis", "utf-8")
	failOnError(err)

	reader := csv.NewReader(converter)

	for {
		record, err := reader.Read() // 1行読み出す
		if err == io.EOF {
			break
		} else {
			failOnError(err)
		}

		log.Printf("%#v", record)
	}
}

実行する

go run main.go

結果

2013/12/18 18:48:41 []string{"区", "男性人口", "女性人口"}
2013/12/18 18:48:41 []string{"中央区", "202,213", "102,080"}
2013/12/18 18:48:41 []string{"花見川区", "179,698", "89,369"}
2013/12/18 18:48:41 []string{"稲毛区", "156,612", "78,199"}
2013/12/18 18:48:41 []string{"若葉区", "151,165", "76,159"}
2013/12/18 18:48:41 []string{"緑区", "125,956", "62,042"}
2013/12/18 18:48:41 []string{"美浜区", "148,951", "72,806"}

ちなみにiconvを外すと下記のようにSJISのまま読み込まる:

2013/12/18 18:50:52 []string{"��", "�j���l��", "�����l��"}
2013/12/18 18:50:52 []string{"������", "202,213", "102,080"}
2013/12/18 18:50:52 []string{"�Ԍ�����", "179,698", "89,369"}
2013/12/18 18:50:52 []string{"���ы�", "156,612", "78,199"}
2013/12/18 18:50:52 []string{"���t��", "151,165", "76,159"}
2013/12/18 18:50:52 []string{"�\u038b�", "125,956", "62,042"}
2013/12/18 18:50:52 []string{"���l��", "148,951", "72,806"}

次回: Go言語: SJISのCSVを読み込みながら1行毎に処理を実行する

33
34
4

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
33
34

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?