从百度统计后台导出并下载了实时访客的 CSV 文件。想本地解析一下,并导入到 MySQL 中做备份,因为百度统计只能保留两个周的历史数据,且上限为5000条。
golang 解析 csv 代码
这里用到了 golang 内置的 "encoding/csv" 库,能自动读取一行 csv 数据,并返回 slice 类型的数据。
package main
import (
"encoding/csv"
"fmt"
iconv "github.com/djimenez/iconv-go"
"io"
"log"
"os"
)
func main() {
// Open the file
csvfile, err := os.Open("2020-05-21.csv")
if err != nil {
log.Fatalln("Couldn't open the csv file", err)
}
defer csvfile.Close()
// Parse the file
r := csv.NewReader(csvfile)
// Iterate through the records
for {
// Read each record from csv
record, err := r.Read()
if err == io.EOF {