我使用golang从谷歌api解析csv文件,文件以utf-16编码,下面的代码尝试读取一条记录(跳过标题)并打印记录,但它给我的输出像这很奇怪:
sample
我猜它可能与utf-16编码有关,但不知道细节,这里是代码: 包主要
, v=/09/20 00:35:42 k=Smartfren Andromax AD681H
答案 0 :(得分:3)
如您所料,输入数据必须从UTF-16编码字符流转换为UTF-8编码字符。您可以使用Go子存储库包golang.org/x/text/encoding/unicode
:
package main
import (
"encoding/csv"
"io"
"log"
"net/http"
"strings"
"golang.org/x/text/encoding/unicode"
)
var url = "http://storage.googleapis.com/play_public/supported_devices.csv"
func main() {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
dec := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()
reader := dec.Reader(resp.Body)
r := csv.NewReader(reader)
r.LazyQuotes = true
r.FieldsPerRecord = -1
// skip header
r.Read()
m := make(map[string]string)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Println(err)
continue
}
if len(record) >= 4 {
m[strings.TrimSpace(record[3])] = strings.TrimSpace(record[1])
for k, v := range m {
log.Printf("k=%s, v=%s\n", k, v)
}
break
}
}
}