Home · RSS · E-Mail · GitHub · GitLab · Mastodon · Twitter · LinkedIn

Working with CSV datasets in Go

first published:

See the repository for the current version!

» Installation

1
go get -u github.com/sj14/csvutil

» Examples

For all options please check the godoc. The examples ignore all error handling!


» Create a new dataset

All datasets should contain a header for further processing.

1
2
3
4
5
6
7
8
records := [][]string{
    {"first_name", "last_name", "username"},
    {"Rob", "Pike", "rob"},
    {"Ken", "Thompson", "ken"},
    {"Robert", "Griesemer", "gri"},
}

ds := csvutil.New(records)

» Add a new row

1
ds.AddRow([]string{"my first name", "my last name", "my nick"})
1
2
3
4
5
first_name,last_name,username
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri
my first name,my last name,my nick

» Add multiple rows

1
2
3
4
5
ds.AddRows([][]string{
    {"my first name 0", "my last name 0", "my nick 0"},
    {"my first name 1", "my last name 1", "my nick 1"},
    {"my first name 2", "my last name 2", "my nick 2"},
})
1
2
3
4
5
6
7
first_name,last_name,username
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri
my first name 0,my last name 0,my nick 0
my first name 1,my last name 1,my nick 1
my first name 2,my last name 2,my nick 2

» Add a new column at index 1

1
2
ds.AddCol([]string{"column_headline", "my row 1", "my row 2", "my row 3"}, 1)
ds.Write(os.Stdout)
1
2
3
4
first_name,column_headline,last_name,username
Rob,my row 1,Pike,rob
Ken,my row 2,Thompson,ken
Robert,my row 3,Griesemer,gri

» Extract Column

1
2
lastNames, _ := ds.ExtractCol("last_name")
fmt.Println(lastNames)
1
[last_name Pike Thompson Griesemer]

» Rename Column

1
ds.RenameCol("username", "nick")
1
2
3
4
first_name,last_name,nick
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri

» Delete Column

1
ds.DeleteCol("first_name")
1
2
3
4
last_name,nick
Pike,rob
Thompson,ken
Griesemer,gri

» Modify Column

1
2
3
addRowNumber := func(val string, i int) string { return fmt.Sprintf("%v (%v)", val, i) }
ds.ModifyCol("first_name", addRowNumber)
ds.Write(os.Stdout)
1
2
3
4
first_name,last_name,username
Rob (1),Pike,rob
Ken (2),Thompson,ken
Robert (3),Griesemer,gri

» Write the dataset

1
ds.Write(os.Stdout)
1
2
3
4
first_name,last_name,username
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri

» Write options

1
ds.Write(os.Stdout, csvutil.Delimiter('|'), csvutil.UseCLRF(true))
1
2
3
4
first_name|last_name|username
Rob|Pike|rob
Ken|Thompson|ken
Robert|Griesemer|gri

» Get dataset as [][]string

1
fmt.Println(ds.Raw())
1
[[first_name last_name username] [Rob Pike rob] [Ken Thompson ken] [Robert Griesemer gri]]

» Compare two (raw) datasets

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
a := [][]string{
    {"a", "b", "c"},
    {"d", "e", "f"},
}

b := [][]string{
    {"g", "h", "i"},
    {"j", "k", "l"},
}

result := csvutil.Equals(a, b)
fmt.Println(result)
1
false



Home · RSS · E-Mail · GitHub · GitLab · Mastodon · Twitter · LinkedIn