Golang : Read tab delimited file with encoding/csv package
This tutorial is just a slight modification from previous tutorial on how to read CSV file and save the result to JSON format file. In this tutorial, we will learn how to instruct the Golang's CSV reader to recognize the tab character '\t'
instead of ,
comma as separator.
To do that, we need to change the type Reader.Comma (https://golang.org/pkg/encoding/csv/#Reader) default value from ','
to '\t'
with this line :
reader.Comma = '\t' // Use tab-delimited instead of comma
and if you are looking to write out tab delimited file instead of comma, remember to change the default value for type Writer.Comma (https://golang.org/pkg/encoding/csv/#Writer) as well.
Let's see how it goes.
We will process the tabdata.csv file
Adam 36 CEO
Eve 34 CFO
Mike 38 COO
with this code :
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strconv"
)
type Employee struct {
Name string
Age int
Job string
}
func main() {
// read data from CSV file
csvFile, err := os.Open("./tabdata.csv")
if err != nil {
fmt.Println(err)
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
reader.Comma = '\t' // Use tab-delimited instead of comma <---- here!
reader.FieldsPerRecord = -1
csvData, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var oneRecord Employee
var allRecords []Employee
for _, each := range csvData {
oneRecord.Name = each[0]
oneRecord.Age, _ = strconv.Atoi(each[1]) // need to cast integer to string
oneRecord.Job = each[2]
allRecords = append(allRecords, oneRecord)
}
jsondata, err := json.Marshal(allRecords) // convert to JSON
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// sanity check
// NOTE : You can stream the JSON data to http service as well instead of saving to file
fmt.Println(string(jsondata))
// now write to JSON file
jsonFile, err := os.Create("./data.json")
if err != nil {
fmt.Println(err)
}
var oneRecord Employee
var allRecords []Employee
for _, each := range csvData {
oneRecord.Name = each[0]
oneRecord.Age, _ = strconv.Atoi(each[1]) // need to cast integer to string
oneRecord.Job = each[2]
allRecords = append(allRecords, oneRecord)
}
jsondata, err := json.Marshal(allRecords) // convert to JSON
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// sanity check
// NOTE : You can stream the JSON data to http service as well instead of saving to file
fmt.Println(string(jsondata))
// now write to JSON file
jsonFile, err := os.Create("./data.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
jsonFile.Write(jsondata)
jsonFile.Close()
}
run the code above and the output will be stored in the data.json file with the following data :
[{"Name":"Adam","Age":36,"Job":"CEO"},{"Name":"Eve","Age":34,"Job":"CFO"},{"Name":"Mike","Age":38,"Job":"COO"}]
Hope this is useful to you.
References :
https://www.socketloop.com/tutorials/golang-convert-csv-data-to-json-format-and-save-to-file
See also : Convert JSON to CSV in Golang
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.1k Unix/Linux/MacOSx : How to remove an environment variable ?
+5.1k Python : Convert(cast) string to bytes example
+10.3k Generate Random number with math/rand in Go
+17.8k Golang : Convert IPv4 address to decimal number(base 10) or integer
+11.7k Golang : Detect user location with HTML5 geo-location
+14.2k Golang : GUI with Qt and OpenCV to capture image from camera
+27.8k Golang : Connect to database (MySQL/MariaDB) server
+8.5k Golang : Random integer with rand.Seed() within a given range
+3.8k Detect if Google Analytics and Developer Media are loaded properly or not
+9.9k Golang : Use regular expression to get all upper case or lower case characters example
+13.5k Golang : Convert spaces to tabs and back to spaces example
+21.4k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload