Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Modify CSV in Go
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"os"
"time"
)
func main() {
filename := "infile.csv"
fi, err := os.Open(filename)
if err != nil {
log.Fatalf("Error opening in file: %v", err)
}
defer fi.Close()
fo, err := os.Create("outfile.csv")
if err != nil {
log.Fatalf("Error opening out file: %v", err)
}
defer fo.Close()
reader := csv.NewReader(bufio.NewReader(fi))
writer := csv.NewWriter(bufio.NewWriter(fo))
lines := 0
linelimit := 10
flushCounter := 0
flushLimit := 100
m := make(map[string]string)
m["StringToReplace1"] = "Test1"
m["StringToReplace2"] = "Test2"
tBefore := time.Now()
for {
line, error := reader.Read()
if error == io.EOF {
break
} else if error != nil {
log.Fatal(error)
}
// Replace cells.
line = ReplaceIndex(line, 3, m)
line = ReplaceIndex(line, 6, m)
// Drop columns.
line = RemoveIndex(line, 6)
line = RemoveIndex(line, 10)
writer.Write(line)
// Flush based on value.
flushCounter++
if flushCounter == flushLimit {
writer.Flush()
flushCounter = 0
}
// Break based on value.
lines++
if lines == linelimit {
break
}
}
// Flush one last time.
writer.Flush()
fmt.Printf("Lines: %v\n", lines)
fmt.Printf("Finished in: %v\n", time.Since(tBefore))
}
func RemoveIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}
func ReplaceIndex(s []string, index int, m map[string]string) []string {
v := s[index]
out := m[v]
s[index] = out
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.