Created
September 5, 2021 12:04
-
-
Save 2minchul/58128818d774bf8ee954180d139bb33b to your computer and use it in GitHub Desktop.
csv_to_ndjson written in go
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 ( | |
"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