golang csv读取压缩字典

大标 2022年3月16日15:08:14
评论
16
package main

import (
	\"encoding/csv\"
	\"fmt\"
	\"os\"
	\"unsafe\"
)

func readCsv(fPath string) (ret []map[string]string) {
	file, err := os.Open(fPath)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()
	reader := csv.NewReader(file)
	reader.FieldsPerRecord = -1
	record, err2 := reader.ReadAll()
	if err2 != nil {
		fmt.Println(err2)
	}
	var res []map[string]string
	var key []string
	for idx, item := range record {
		if idx == 0 {
			key = item
			//fmt.Println(key)
			continue
		}
		var rowDict map[string]string
		rowDict = make(map[string]string)
		//res = append(res, item)
		for i, v := range item {
			k := key[i]
			rowDict[k] = v
		}
		//fmt.Println(rowDict)
		res = append(res, rowDict)
	}
	return res

}

func zipDict(ret []map[string]string, kCol string) (result map[string][]map[string]string) {
	/*
		类似python 默认字典实现
		{key:[{k:v,k1:v1,k2:v2}]}
	*/
	var retMap map[string][]map[string]string
	retMap = make(map[string][]map[string]string)
	for _, row := range ret {
		//fmt.Println(idx + 1)
		//fmt.Println(row[\"credit_code\"], row[\"enterprise_name\"])
		kColV := row[kCol]
		var dataList []map[string]string
		// 判断key 是否存在 不存在插入数据 存在获取切片追加数据
		if existDataList, ok := retMap[kColV]; !ok {
			dataList = append(dataList, row)
			retMap[kColV] = dataList
		} else {
			//existDataList := retMap[kColV]
			existDataList = append(existDataList, row)
			retMap[kColV] = existDataList
		}
	}
	return retMap
}

func main() {
	
	fileName := \"D:\\\\tmp\\\\20220228\\\\3.csv\"
	data := readCsv(fileName)
	//fmt.Println(unsafe.Sizeof(data))
	zipData := zipDict(data, \"credit_code\")
	fmt.Println(unsafe.Sizeof(zipData))
	//for k, v := range zipData {
	//	fmt.Println(k, v)
	//}
	fmt.Println(len(zipData))
	fmt.Println(zipData[\"91330283MA2AF845XJ\"])
}

  

  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
大标
  • 本文由 发表于 2022年3月16日15:08:14
  • 转载请务必保留本文链接:https://www.tanhuibiao.com/script/qita/4704.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: