Documentation ¶
Overview ¶
Package file provides functionality for reading, processing, and saving CSV files. It includes features for inferring column data types and saving the data to Excel files.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithDelimiter ¶
WithDelimiter sets the delimiter for the CSV struct.
func WithFilePath ¶
WithFilePath sets the file path for the CSV struct.
func WithHeaders ¶
WithHeaders sets the column headers for the CSV struct.
func WithRecords ¶
func WithRecords(records [][]interface{}) func(*CSV)
WithRecords sets the data records for the CSV struct.
Types ¶
type CSV ¶
type CSV struct { // FilePath is the path to the CSV file. FilePath string // Delimiter is the character used to separate fields in the CSV file. Delimiter rune // Columns is a slice of Column information. Headers []Column // Records is a slice of slices, where each inner slice represents a row of data. // The data type of the elements within the inner slices can vary based on type inference. Records [][]interface{} }
CSV represents a CSV file and its parsed data.
func (*CSV) ConvertColumnTypes ¶
func (c *CSV) ConvertColumnTypes()
ConvertColumnTypes attempts to convert string values in the Records to their inferred types (float or integer). This function relies on the inferColumnTypes method to determine the appropriate type for each column.
func (*CSV) GetHeaderNames ¶
GetHeaderNames returns a Slice with the names of the columns in the CSV file.
func (*CSV) InferColumnTypes ¶
func (c *CSV) InferColumnTypes()
inferColumnTypes analyzes a sample of rows to infer the data type of each column. It checks if the values in a column can be parsed as float or integer. The number of rows to inspect is determined by the defaultTypeInferanceRows constant.
func (*CSV) Read ¶
Read reads the CSV file, parses its contents, and populates the CSV struct. It infers column names from the first row and stores the data in the Records field. Returns an error if the file cannot be opened or read.
func (*CSV) SaveAsExcel ¶
SaveAsExcel saves the CSV data to an Excel file. It creates a new Excel file and writes the column names and data records to the specified sheet. Returns an error if the file cannot be created or written to.
type Column ¶
type Column struct { // Name is the name of the column, typically read from the header row. Name string // Type is the inferred data type of the column. Type ColumnType }
Column represents a column in the CSV file, including its name and inferred data type.
type ColumnType ¶
type ColumnType int
const ( StringType ColumnType = iota + 1 FloatType IntegerType )