Skip to main content

Command Palette

Search for a command to run...

parsing comma-separated data

Updated
2 min readView as Markdown
parsing comma-separated data
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources
  • There are multiple table data formats. CSV (comma-separated values) is one of the most basic formats largely used for data transport and export. There is no standard that defines CSV, but the format itself is described in RFC 4180.

  • This recipe introduces how to parse CSV-formatted data comfortably.

Create a file named data.csv with the following content:

"Name","Surname","Age"
# this is comment in data
"sangam","biradar",24
arjun,singh,21

Create the main.go file with the following content:


        package main

        import (
          "encoding/csv"
          "fmt"
          "os"
        )

        func main() {

          file, err := os.Open("data.csv")
          if err != nil {
            panic(err)
          }
          defer file.Close()

          reader := csv.NewReader(file)
          reader.FieldsPerRecord = 3
          reader.Comment = '#'

          for {
            record, e := reader.Read()
            if e != nil {
              fmt.Println(e)
              break
            }
            fmt.Println(record)
          }
        }

output:

sangam:golang-daily sangam$ go run main.go
[Name Surname Age]
[sangam biradar 24]
[arjun singh 21]
EOF
sangam:golang-daily sangam$

Create a file named data_uncommon.csv with the following content:

Name;Surname;Age
"sangam";biradar;24
"arjun";singh;21

Create a file named main.go with the following content:

package main

import (
    "encoding/csv"
    "fmt"
    "os"
)

func main() {

    file, err := os.Open("data_uncommon.csv")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    reader := csv.NewReader(file)
    reader.Comma = ';'

    for {
        record, e := reader.Read()
        if e != nil {
            fmt.Println(e)
            break
        }
        fmt.Println(record)
    }
}

output:

sangam:golang-daily sangam$ go run main.go
[Name Surname Age]
[sangam biradar 24]
[arjun singh 21]
EOF
sangam:golang-daily sangam$

How it works...

  • Instead of simply scanning the input line by line and using strings.Split and other methods to parse the CSV format, Go offers a better way. The NewReader function in the encoding/csv package returns the Reader structure, which provides the API to read the CSV file. The Reader struct keeps variables to configure the read parameters, according to your needs.

  • The FieldsPerRecord parameter of Reader is a significant setting. This way the cell count per row could be validated. By default, when set to 0, it is set to the number of records in a first line. If a positive value is set, the number of records must match. If a negative value is set, there is no cell count validation.

  • Another interesting configuration is the Comment parameter, which allows you to define the comment characters in the parsed data. In the example, a whole line is ignored this way.

21 views

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !