Created
March 23, 2018 21:09
-
-
Save josephspurrier/50681f7f4c3b17d5fcf8f898bc863619 to your computer and use it in GitHub Desktop.
Modify CSV in Go
This file contains hidden or 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 ( | |
"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