Documentation ¶
Overview ¶
Package csvutil exposes functions and types to easily handle CSV files. csvutil builds upon the encoding/csv package and provides the Table type. A Table can read data from a CSV file into a struct value whose fields are the various columns of the CSV file. Conversely, a Table can write data into a CSV file from a struct value.
Example (ReadSlice) ¶
fname := "testdata/simple.csv" tbl, err := Open(fname) if err != nil { log.Fatalf("could not open %s: %v\n", fname, err) } defer tbl.Close() tbl.Reader.Comma = ';' tbl.Reader.Comment = '#' rows, err := tbl.ReadRows(0, 10) if err != nil { log.Fatalf("could read rows [0, 10): %v\n", err) } defer rows.Close() irow := 0 for rows.Next() { var ( I int F float64 S string ) err = rows.Scan(&I, &F, &S) if err != nil { log.Fatalf("error reading row %d: %v\n", irow, err) } } err = rows.Err() if err != nil { log.Fatalf("error: %v\n", err) }
Output:
Example (ReadStruct) ¶
fname := "testdata/simple.csv" tbl, err := Open(fname) if err != nil { log.Fatalf("could not open %s: %v\n", fname, err) } defer tbl.Close() tbl.Reader.Comma = ';' tbl.Reader.Comment = '#' rows, err := tbl.ReadRows(0, 10) if err != nil { log.Fatalf("could read rows [0, 10): %v\n", err) } defer rows.Close() irow := 0 for rows.Next() { data := struct { I int F float64 S string }{} err = rows.Scan(&data) if err != nil { log.Fatalf("error reading row %d: %v\n", irow, err) } } err = rows.Err() if err != nil { log.Fatalf("error: %v\n", err) }
Output:
Example (WriteSlice) ¶
fname := "out.csv" tbl, err := Create(fname) if err != nil { log.Fatalf("could not create %s: %v\n", fname, err) } defer tbl.Close() tbl.Writer.Comma = ';' err = tbl.WriteHeader("## a simple set of data: int64;float64;string\n") if err != nil { log.Fatalf("error writing header: %v\n", err) } for i := 0; i < 10; i++ { var ( f = float64(i) s = fmt.Sprintf("str-%d", i) ) err = tbl.WriteRow(i, f, s) if err != nil { log.Fatalf("error writing row %d: %v\n", i, err) } } err = tbl.Close() if err != nil { log.Fatalf("error closing table: %v\n", err) }
Output:
Example (WriteStruct) ¶
fname := "out.csv" tbl, err := Create(fname) if err != nil { log.Fatalf("could not create %s: %v\n", fname, err) } defer tbl.Close() tbl.Writer.Comma = ';' err = tbl.WriteHeader("## a simple set of data: int64;float64;string\n") if err != nil { log.Fatalf("error writing header: %v\n", err) } for i := 0; i < 10; i++ { data := struct { I int F float64 S string }{ I: i, F: float64(i), S: fmt.Sprintf("str-%d", i), } err = tbl.WriteRow(data) if err != nil { log.Fatalf("error writing row %d: %v\n", i, err) } } err = tbl.Close() if err != nil { log.Fatalf("error closing table: %v\n", err) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows is an iterator over an interval of rows inside a CSV file.
func (*Rows) Close ¶
Close closes the Rows, preventing further enumeration. Close is idempotent and does not affect the result of Err.
func (*Rows) Err ¶
Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.
func (*Rows) Fields ¶
Fields returns the raw string values of the fields of the current CSV-record. Fields assumes Rows.Next() has been called at least once.
func (*Rows) Next ¶
Next prepares the next result row for reading with the Scan method. It returns true on success, false if there is no next result row. Every call to Scan, even the first one, must be preceded by a call to Next.
type Table ¶
type Table struct { Reader *csv.Reader Writer *csv.Writer // contains filtered or unexported fields }
Table provides read- or write-access to a CSV file. Table supports reading and writing data to/from a struct value.
func Append ¶
Append opens an already existing CSV file and returns a Table in write mode. The file cursor is positioned at the end of the file so new data can be appended via the returned Table.
func (*Table) ReadRows ¶
ReadRows returns a row iterator semantically equivalent to [beg,end). If end==-1, the iterator will be configured to read rows until EOF.
func (*Table) WriteHeader ¶
WriteHeader writes a header to the underlying CSV file