func (obj *Idcsv) load(t *tool.TFile) { headStr := t.NextLine(0) //the first line headArr := strings.Split(*headStr, ",") num := len(headArr) if num < 2 { fmt.Println("error format for dict") return } L: for { lineStr := t.Next() if lineStr == nil { break L } lineArr := strings.Split(*lineStr, ",") if len(lineArr) != num { fmt.Printf("error format string #%v#, bad num #%v#%v#\n", lineStr, len(lineArr), num) continue } for idx, str := range headArr { if str != "" || lineArr[idx] == "" { continue } obj.addItem(lineArr[idx-1], strings.Trim(lineArr[idx], " ")) } } }
func (r *RoutEx) loadMap(t *tool.TFile) map[int]string { m := make(map[int]string) L: for { lineStr := t.Next() if lineStr == nil { break L } // lineArr := strings.Split(*lineStr, ",") locked := false lineArr := strings.FieldsFunc(*lineStr, func(ru rune) bool { rlt := false switch ru { case ',': if !locked { rlt = true } case '"': locked = !locked } return rlt }) if len(lineArr) != 2 { fmt.Printf("dict-ex.go error #%v#\n", len(lineArr)) continue } keyStr := strings.TrimFunc(lineArr[0], tool.NumberNF) keyInt, err := strconv.Atoi(keyStr) if err != nil { fmt.Printf("dict-ex error key : #%v#\n", keyStr) continue } if m[keyInt] != "" { fmt.Printf("had : %v", m[keyInt]) continue } m[keyInt] = strings.Trim(lineArr[1], "\"") } return m }
// End used to func (r *Rout) End(ch core.RoutineChan) { var t *tool.TFile var ok bool if t, ok = ch.(*tool.TFile); !ok { fmt.Println("dict-csv.go error") return } headStr := t.NextLine(0) //the first line headArr := strings.Split(*headStr, ",") num := len(headArr) if num < 2 { fmt.Println("error format for dict") return } L: for { lineStr := t.Next() if lineStr == nil { break L } lineArr := make([]string, num) formatKey([]rune(*lineStr), lineArr) if len(lineArr) != num { fmt.Printf("error format string #%v#, bad num #%v#%v#\n", *lineStr, len(lineArr), num) continue } for idx, str := range headArr { if str != "" || lineArr[idx] == "" { continue } addItem(r.Dict, lineArr[idx-1], strings.Trim(lineArr[idx], " ")) } } }