Example #1
0
File: main.go Project: foolin/gocsv
func main() {

	var err error

	//======================= read map[string]interface{} ===================//
	fmt.Println("\n------------- read  -------------")
	//datautf8.csv utf8 file
	data, err := gocsv.Read("datautf8.csv", false)
	if err != nil {
		panic(fmt.Sprintf("read error: %v", err))
		return
	}
	fmt.Printf("%#v\n", data)

	//======================= read list struct ===================//
	fmt.Println("\n------------- read list struct -------------")
	var list1 []Goods
	//data.csv ANSI(excel default)
	err = gocsv.ReadList("data.csv", true, &list1)
	if err != nil {
		fmt.Printf("read error: %v", err)
		return
	}
	fmt.Printf("%#v\n", list1)

	//======================= read list ptr ===================//
	fmt.Println("\n------------- read list ptr  -------------")
	var list2 []*Goods
	//data.csv ANSI(excel default)
	err = gocsv.ReadList("data.csv", true, &list2)
	if err != nil {
		fmt.Printf("read error: %v", err)
		return
	}
	fmt.Printf("%#v\n", list2)

	//======================= read map struct ===================//
	fmt.Println("\n------------- read map struct -------------")
	var map1 map[int]Goods
	//data.csv ANSI(excel default)
	err = gocsv.ReadMap("data.csv", true, "id", &map1)
	if err != nil {
		fmt.Printf("read error: %v", err)
		return
	}
	fmt.Printf("%#v\n", map1)

	//======================= read map ptr ===================//
	fmt.Println("\n------------- read map ptr  -------------")
	var map2 map[int]*Goods
	//data.csv ANSI(excel default)
	err = gocsv.ReadMap("data.csv", true, "id", &map2)
	if err != nil {
		fmt.Printf("read error: %v", err)
		return
	}
	fmt.Printf("%#v\n", map2)

	//======================= read parser ===================//
	fmt.Println("\n------------- read parser  -------------")
	line := 1
	err = gocsv.ReadRaw("data.csv", true, func(fields []gocsv.Field) error {
		fmt.Printf("-line %v\n", line)
		for _, f := range fields {
			fmt.Printf("%#v\n", f)
		}
		line = line + 1
		return nil
	})
	if err != nil {
		fmt.Printf("read error: %v", err)
		return
	}

}
Example #2
0
func main() {
	//abs, err := filepath.Abs("./../")
	//log.Printf(filepath.Base(abs))
	flag.Parse()
	if *csvpath == "" {
		flag.Usage()
		return
	}
	fileInfo, err := os.Stat(*csvpath)
	if err != nil {
		log.Panic(err)
		return
	}
	isOutOneFile := false
	if *outpath != "" && strings.ToLower(filepath.Ext(*outpath)) == ".json" {
		isOutOneFile = true
	}
	if *outpath == "" {
		*outpath = *csvpath
	}

	mapAllList := make(map[string]interface{}, 0)
	if fileInfo.IsDir() {
		infos, err := ioutil.ReadDir(*csvpath)
		if err != nil {
			log.Panic(err)
			return
		}
		if *outpath == "" {
			*outpath = *csvpath
		}
		for _, info := range infos {
			ext := filepath.Ext(info.Name())
			if ext != ".csv" {
				continue
			}

			name := filename(info.Name())
			list, err := gocsv.Read(path.Join(*csvpath, info.Name()), true)
			if err != nil {
				log.Fatalf("read csv: %v, error: %v", info.Name(), err)
				return
			}
			if isOutOneFile {
				mapAllList[name] = list
			} else {
				jsonfile := strings.Replace(info.Name(), ".csv", ".json", -1)
				outFile := path.Join(*outpath, jsonfile)
				err = writeJsonFile(outFile, list)
				if err != nil {
					log.Fatalf("write file: %v error: %v", outFile, err)
					return
				}
				log.Printf("write file: %v", outFile)
			}

		}

	} else {
		name := upper(filename(fileInfo.Name()))
		list, err := gocsv.Read(path.Join(*csvpath, fileInfo.Name()), true)
		if err != nil {
			log.Fatalf("read csv error: %v", err)
			return
		}
		if isOutOneFile {
			mapAllList[name] = list
		} else {
			jsonfile := strings.Replace(fileInfo.Name(), ".csv", ".json", -1)
			outFile := path.Join(*outpath, jsonfile)
			err = writeJsonFile(outFile, list)
			if err != nil {
				log.Fatalf("write file: %v error: %v", outFile, err)
				return
			}
			log.Printf("write file: %v", outFile)
		}
	}

	if isOutOneFile {
		outFile := *outpath
		err = writeJsonFile(outFile, mapAllList)
		if err != nil {
			log.Fatalf("write file: %v error: %v", outFile, err)
			return
		}
		log.Printf("write file: %v", outFile)
	}

	log.Print("generator done!")
}