Created
March 26, 2020 19:34
-
-
Save klaygomes/501127375e301724e6983b95906985ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"fmt" | |
"io" | |
"os" | |
"sort" | |
) | |
var desiredColumns = []string{"id", | |
"date_registred", | |
"channel" | |
} | |
func indexOf(origin []string, column string) int { | |
for i, v := range origin { | |
if column == v { | |
return i | |
} | |
} | |
return -1 | |
} | |
func intersectMap(desired []string, columns []string) []int { | |
idx := []int {} | |
for _, v := range columns { | |
if f := indexOf(desired, v); f > 0 { | |
idx = append(idx, f) | |
} | |
} | |
sort.Ints(idx) | |
return idx | |
} | |
func cleanHeader(input []string) []string { | |
ret := []string{} | |
for _, v := range input { | |
ret = append(ret, v[1:]) | |
} | |
return ret | |
} | |
func main(){ | |
f, _ := os.Open("./1-customers.csv") | |
r, _ := os.Create("./ret-1.csv") | |
reader := csv.NewReader(f) | |
columns, _ := reader.Read() | |
line := cleanHeader(columns) | |
desiredIdx := intersectMap(desiredColumns, line) | |
csvWriter := csv.NewWriter(r) | |
i := 0 | |
for { | |
writeLine := []string{} | |
for _, v := range desiredIdx { | |
writeLine = append(writeLine, line[v]) | |
} | |
if i== 0{ | |
fmt.Println(writeLine) | |
} | |
i++ | |
csvWriter.Write(writeLine) | |
var err error | |
line, err = reader.Read() | |
if err == io.EOF { | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment