Skip to content

Instantly share code, notes, and snippets.

@2minchul
Created September 5, 2021 12:04
Show Gist options
  • Save 2minchul/58128818d774bf8ee954180d139bb33b to your computer and use it in GitHub Desktop.
Save 2minchul/58128818d774bf8ee954180d139bb33b to your computer and use it in GitHub Desktop.
csv_to_ndjson written in go
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"os"
)
func main() {
file, err := os.Open("./in.csv")
if err != nil {
println("there is no in.csv")
panic(err)
}
defer file.Close()
rdr := csv.NewReader(bufio.NewReader(file))
header, _ := rdr.Read()
wf, err := os.Create("./out.ndjson")
if err != nil {
panic(err)
}
defer wf.Close()
for {
row, err := rdr.Read()
if err != nil {
break
}
d := map[string]interface{}{}
for i, key := range header {
d[key] = row[i]
}
b, _ := json.Marshal(d)
_, err = wf.Write(b)
if err != nil {
panic(err)
}
_, _ = wf.Write([]byte{'\n'})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment