file

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2025 License: MIT Imports: 6 Imported by: 0

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

func WithDelimiter(delimiter rune) func(*CSV)

WithDelimiter sets the delimiter for the CSV struct.

func WithFilePath

func WithFilePath(filePath string) func(*CSV)

WithFilePath sets the file path for the CSV struct.

func WithHeaders

func WithHeaders(columns []Column) func(*CSV)

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 Merge

func Merge(files ...*CSV) (*CSV, error)

func New

func New(options ...func(*CSV)) *CSV

New creates a new CSV struct with the specified options.

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

func (c *CSV) GetHeaderNames() []string

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

func (c *CSV) Read() error

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

func (c *CSV) SaveAsExcel(filePath string, sheetName string) error

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
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL