Documentation
¶
Overview ¶
© 2017 Osvaldo Gago
The Go package simplecsv is a simple mini-library to handle csv files. I'm building it to help me writing small command line scripts. Maybe it's useful to someone else as well.
Some notes:
- all read methods return the value in the csv and a second true/false value that is true if the value exists
- all write methods that change the csv return the changed csv and a true/false value if the operation was sucessful
- all cells are strings
- Simplecsv works with comma separated csv files.
* CSV FILE *
READ ¶
Reads file and parses as a SimpleCsv object. `fileRead` is false if there's an error reading the file or parsing the CSV.
var x simplecsv.SimpleCsv
x, fileRead = simplecsv.ReadCsvFile("my1file.csv")
CREATE ¶
Create empty file and define cssv headers:
var u simplecsv.SimpleCsv
u = simplecsv.CreateEmpyCsv([]string{"Age", "Gender", "ID"})
WRITE ¶
Write the SimpleCsv object to my2file.csv. If there's an error, `wasWritten` is false.
wasWritten := u.WriteCsvFile("my2file.csv")
* HEADERS *
The cells of the first row are considered headers.
GET ¶
Get all headers:
headers := x.GetHeaders()
Get header at position one (second position as it starts from 0):
headerName, headerExists := x.GetHeader(1)
Get header position: (it returns `-1` if the header does not exist)
position := x.GetHeaderPosition("Gender")
RENAME ¶
Rename header: (old header, new header)
x, headerExists := x.RenameHeader("ID", "IDnumber")
`headerExists` is false if the old header does not exist.
* ROWS *
GET ¶
Get number of rows:
numberOfRows := x.GetNumberRows()
Get second row:
row, rowExists := x.GetRow(1)
Get second row as a map:
row, rowExists := x.GetRowAsMap(1)
ADD ¶
Add a slice to a row. The slice must have the same size as the CSV number of columns. If not wasSucessful is false.
x, wasSucessful = x.AddRow([]string{"24", "M", "2986732"})
Add row from map: (If the map keys don't exist as columns, the value will be discarded. If a key does not exist, it will create empty cells.)
mymap := make(map[string]string) mymap["Age"] = "62" mymap["Gender"] = "F" mymap["ID"] = "6463246" x, wasAdded = x.AddRowFromMap(mymap)
SET ¶
Set second row (1) from a slice. The length of the slice must be the same as the number of columns and the row must already exist. If there’s an error `wasSet` is false.
x, wasSet = x.SetRow(1, []string{"45", "F", "8356138"})
Set second row from map: If the map keys don't exist as columns, the value will be discarded. If a key does not exist, it will create empty cells.)
mymap2 := make(map[string]string) mymap2["Age"] = "62" mymap2["Gender"] = "F" mymap2["ID"] = "6463246" x, wasAdded = x.SetRowFromMap(1, mymap2)
DELETE ¶
Delete second row: (If the row number is invalid, `wasDeleted` is false)
x, wasDeleted = x.DeleteRow(1)
* CELLS *
.
Index ¶
- func AndIndex(indexes ...[]int) []int
- func NotIndex(index []int, min, max int) []int
- func OrIndex(indexes ...[]int) []int
- type SimpleCsv
- func (s SimpleCsv) AddEmptyColumn(columnName string) (SimpleCsv, bool)
- func (s SimpleCsv) AddRow(rowValue []string) (SimpleCsv, bool)
- func (s SimpleCsv) AddRowFromMap(rowValue map[string]string) (SimpleCsv, bool)
- func (s SimpleCsv) DeleteRow(rowNumber int) (SimpleCsv, bool)
- func (s SimpleCsv) FindInColumn(columnPosition int, word string) ([]int, bool)
- func (s SimpleCsv) FindInField(columnName string, word string) ([]int, bool)
- func (s SimpleCsv) GetCell(column int, row int) (string, bool)
- func (s SimpleCsv) GetCellByField(columnName string, row int) (string, bool)
- func (s SimpleCsv) GetHeader(columnPosition int) (string, bool)
- func (s SimpleCsv) GetHeaderPosition(columnName string) int
- func (s SimpleCsv) GetHeaders() []string
- func (s SimpleCsv) GetNumberRows() int
- func (s SimpleCsv) GetRow(rowNumber int) ([]string, bool)
- func (s SimpleCsv) GetRowAsMap(rowNumber int) (map[string]string, bool)
- func (s SimpleCsv) MatchInColumn(columnPosition int, regularexpression string) ([]int, bool)
- func (s SimpleCsv) MatchInField(columnName string, regularexpression string) ([]int, bool)
- func (s SimpleCsv) OnlyThisFields(fields []string) (SimpleCsv, bool)
- func (s SimpleCsv) OnlyThisRows(rowsIndex []int, header bool) (SimpleCsv, bool)
- func (s SimpleCsv) RemoveColumn(columnPosition int) (SimpleCsv, bool)
- func (s SimpleCsv) RemoveColumnByName(columnName string) (SimpleCsv, bool)
- func (s SimpleCsv) RenameHeader(oldHeader string, newHeader string) (SimpleCsv, bool)
- func (s SimpleCsv) SetCell(column int, row int, value string) (SimpleCsv, bool)
- func (s SimpleCsv) SetCellByField(columnName string, row int, value string) (SimpleCsv, bool)
- func (s SimpleCsv) SetRow(rowNumber int, rowValue []string) (SimpleCsv, bool)
- func (s SimpleCsv) SetRowFromMap(rowNumber int, rowValue map[string]string) (SimpleCsv, bool)
- func (s SimpleCsv) UpdateRowCellsFromMap(rowNumber int, rowValue map[string]string) (SimpleCsv, bool)
- func (s SimpleCsv) WriteCsvFile(filename string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type SimpleCsv ¶
type SimpleCsv [][]string
SimpleCsv is the type for simple csv
func CreateEmpyCsv ¶
CreateEmpyCsv creates an empty CSV with the headers passed as a slice
func ReadCsvFile ¶
ReadCsvFile reads a file and returns a [][]string slice
func (SimpleCsv) AddEmptyColumn ¶
AddEmptyColumn ads an empty column at the end of the csv
func (SimpleCsv) AddRowFromMap ¶
AddRowFromMap adds map values to a row. Ignores keys with unexistiging headers Fills blank where the key does not exists
func (SimpleCsv) FindInColumn ¶
FindInColumn returns a slice with the rownumbers where the "word" is in the columnPosition If the column is not valid it returns an empty slice and a second false value
func (SimpleCsv) FindInField ¶
FindInField returns a slice with the rownumbers where the "word" is in the column name If the column is not valid it returns an empty slice and a second false value
func (SimpleCsv) GetCell ¶
GetCell Returns the value of a cell by position. If the cell does not exist, it returns the second value as false.
func (SimpleCsv) GetCellByField ¶
GetCellByField returns the value of a cell by column name. If the cell does not exist, it returns the second value as false.
func (SimpleCsv) GetHeader ¶
GetHeader returns the header name in a position Returns false as the second value if it does not exist
func (SimpleCsv) GetHeaderPosition ¶
GetHeaderPosition returns the header position, returns -1 if it does not exist
func (SimpleCsv) GetHeaders ¶
GetHeaders returns the headers as a slice
func (SimpleCsv) GetNumberRows ¶
GetNumberRows returns the number of rows, including the header row
func (SimpleCsv) GetRow ¶
GetRow returns the row rowNumber If rowNumber does not exist, it returns an empty slice and false
func (SimpleCsv) GetRowAsMap ¶
GetRowAsMap returns the row as a map If the row does not exist, returns second value as false
func (SimpleCsv) MatchInColumn ¶
MatchInColumn returns a slice with the rownumbers where the regular expression applies in the columnPosition If the column or regular expression are not valid it returns an empty slice and a second false value
func (SimpleCsv) MatchInField ¶
MatchInField returns a slice with the rownumbers where the regular expression applies in the column name
func (SimpleCsv) OnlyThisFields ¶
OnlyThisFields returns a simplecsv with the fields
func (SimpleCsv) OnlyThisRows ¶
OnlyThisRows removes all rows that are not in the index and sorts the csv by the index order All rows must exist or it fails
func (SimpleCsv) RemoveColumn ¶
RemoveColumn removes the column in position X
func (SimpleCsv) RemoveColumnByName ¶
RemoveColumnByName removes column by name
func (SimpleCsv) RenameHeader ¶
RenameHeader renames the header Returns the second value as false if it did't found the oldHeader
func (SimpleCsv) SetCellByField ¶
SetCellByField changes the value of a cell with a specific column
func (SimpleCsv) SetRowFromMap ¶
SetRowFromMap replaces a row by the maps value Ignores keys with unexistiging headers Fills blank where the key does not exists
func (SimpleCsv) UpdateRowCellsFromMap ¶
func (s SimpleCsv) UpdateRowCellsFromMap(rowNumber int, rowValue map[string]string) (SimpleCsv, bool)
UpdateRowCellsFromMap replaces a row by the maps value Ignores keys with unexistiging headers Fills the old value where the key does not exist
func (SimpleCsv) WriteCsvFile ¶
WriteCsvFile writes the slice to a file