Documentation ¶
Overview ¶
Package csvdict is a Short Go library that extends encoding/csv to handle importing CSV content with headers as a map (dictionary)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DictReader ¶
type DictReader struct {
// contains filtered or unexported fields
}
DictReader contains a reference to a CSV reader and a header row slice, used to map each CSV row to its headers
Example ¶
package main import ( "fmt" "io" "log" "strings" "github.com/kurankat/csvdict" ) func main() { in := `name, age, occupation "Mark Smith",33,"Jack of all trades" "Douglas Adams",42,Writer Methuselah,969,Patriarch ` r, err := csvdict.NewDictReader(strings.NewReader(in)) if err != nil { panic(err) } headers := r.GetHeaderRow() for { record, err := r.Read() if err == io.EOF { break } if err != nil { log.Fatal(err) } fmt.Printf("%v:%v %v:%v %v:%v\n", headers[0], record[headers[0]], headers[1], record[headers[1]], headers[2], record[headers[2]]) } }
Output: name:Mark Smith age:33 occupation:Jack of all trades name:Douglas Adams age:42 occupation:Writer name:Methuselah age:969 occupation:Patriarch
func NewDictReader ¶
func NewDictReader(r io.Reader) (dr *DictReader, err error)
NewDictReader takes an io.Reader as an argument and returns a reference to a DictReader
func (*DictReader) GetHeaderRow ¶
func (d *DictReader) GetHeaderRow() (header []string)
GetHeaderRow returns the header row of the DictReader object
func (*DictReader) Read ¶
func (d *DictReader) Read() (csvMap map[string]string, err error)
Read reads a single line of a CSV file and returns a map where each field is mapped to its header
func (*DictReader) ReadAll ¶
func (d *DictReader) ReadAll() (records []map[string]string, err error)
ReadAll reads all the remaining records from r. Each record is a map of fields. A successful call returns err == nil, not err == io.EOF. Because ReadAll is defined to read until EOF, it does not treat end of file as an error to be reported.
Example ¶
package main import ( "fmt" "log" "strings" "github.com/kurankat/csvdict" ) func main() { in := `name, age, occupation "Mark Smith",33,"Jack of all trades" "Douglas Adams",42,Writer Methuselah,969,Patriarch ` r, err := csvdict.NewDictReader(strings.NewReader(in)) if err != nil { panic(err) } records, err := r.ReadAll() if err != nil { log.Fatal(err) } headers := r.GetHeaderRow() for _, record := range records { fmt.Printf("%v:%v %v:%v %v:%v\n", headers[0], record[headers[0]], headers[1], record[headers[1]], headers[2], record[headers[2]]) } }
Output: name:Mark Smith age:33 occupation:Jack of all trades name:Douglas Adams age:42 occupation:Writer name:Methuselah age:969 occupation:Patriarch
type DictWriter ¶
type DictWriter struct {
// contains filtered or unexported fields
}
DictWriter contains a reference to a CSV Writer object and a header row slice
Example ¶
package main import ( "os" "github.com/kurankat/csvdict" ) func main() { headers := []string{"name", "age", "occupation"} records := []map[string]string{ {"name": "Mark Smith", "age": "33", "occupation": "Jack of all trades"}, {"name": "Douglas Adams", "age": "42", "occupation": "Writer"}, {"name": "Methuselah", "age": "969", "occupation": "Patriarch"}, } w := csvdict.NewDictWriter(os.Stdout, headers) w.WriteHeaders() for _, record := range records { err := w.Write(record) if err != nil { panic(err) } } w.Flush() }
Output: name,age,occupation Mark Smith,33,Jack of all trades Douglas Adams,42,Writer Methuselah,969,Patriarch
func NewDictWriter ¶
func NewDictWriter(w io.Writer, h []string) *DictWriter
NewDictWriter takes an io.Writer and a slice containing the header row as arguments and returns a reference to a DictWriter
func (*DictWriter) Flush ¶
func (w *DictWriter) Flush()
Flush ensures that the writer is flushed to file
func (*DictWriter) Write ¶
func (w *DictWriter) Write(csvMap map[string]string) error
Write takes a map with headers as indices and field contents as values, and writes it to file as CSV
func (*DictWriter) WriteAll ¶
func (w *DictWriter) WriteAll(records []map[string]string) error
WriteAll writes multiple CSV records to w using Write and then calls Flush.
Example ¶
package main import ( "os" "github.com/kurankat/csvdict" ) func main() { headers := []string{"name", "age", "occupation"} records := []map[string]string{ {"name": "Mark Smith", "age": "33", "occupation": "Jack of all trades"}, {"name": "Douglas Adams", "age": "42", "occupation": "Writer"}, {"name": "Methuselah", "age": "969", "occupation": "Patriarch"}, } w := csvdict.NewDictWriter(os.Stdout, headers) err := w.WriteAll(records) if err != nil { panic(err) } }
Output: name,age,occupation Mark Smith,33,Jack of all trades Douglas Adams,42,Writer Methuselah,969,Patriarch
func (*DictWriter) WriteHeaders ¶
func (w *DictWriter) WriteHeaders()
WriteHeaders writes the header row to file