easy_csv

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: MIT Imports: 7 Imported by: 0

README

easy_csv

The easy_csv package is used to marshal or unmarshal csv file data.

Installation

go get -u github.com/CXeon/easy_csv

Full example

Marshal a structure to a csv file.

package main

import (
	"github.com/CXeon/easy_csv"
	"os"
)

type testStudentInfo struct {
	Name  string `csv:"name"`
	Age   int
	Grade string
	Score float64 `csv:"mScore"`
	Email string  `csv:"mEmail,email_desensitization"` //The email_desensitization will desensitize email addresses.The email username must be greater than 3 characters to be effective.
	Phone string  `csv:"mPhone,phone_desensitization"` //The phone_desensitization will desensitize the phone number.The mobile phone number must be greater than 6 digits to be effective.
}

func main() {
	fileName := "./CsvWriter.csv"

	csvFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()

	clientWriter := easy_csv.NewClientWriter(csvFile)

	//clientWriter.WriteString2File()

	row := testStudentInfo{
		Name:  "Jam",
		Age:   10,
		Grade: "Grade 4",
		Score: 99.01,
		Email: "jamjam@test.com",
		Phone: "13322226666",
	}

	// parameter row cloud be &row
	err = clientWriter.WriteRow2File(row, true)
	if err != nil {
		panic(err)
	}

}

The result is

name Age Grade mScore mEmail mPhone
Jam 10 Grade 4 99.01 ja***m@test.com 133****6666

Marshal a list to a csv file.

package main

import (
	"github.com/CXeon/easy_csv"
	"os"
)

type testStudentInfo struct {
	Name  string `csv:"name"`
	Age   int
	Grade string
	Score float64 `csv:"mScore"`
	Email string  `csv:"mEmail,email_desensitization"` //The email_desensitization will desensitize email addresses.The email username must be greater than 3 characters to be effective.
	Phone string  `csv:"mPhone,phone_desensitization"` //The phone_desensitization will desensitize the phone number.The mobile phone number must be greater than 6 digits to be effective.
}

func main() {
	fileName := "./CsvWriter.csv"

	csvFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()

	clientWriter := easy_csv.NewClientWriter(csvFile)

	list := []testStudentInfo{
		{
			Name:  "Jam",
			Age:   10,
			Grade: "Grade 4",
			Score: 99.01,
			Email: "jamjam@test.com",
			Phone: "13322226666",
		},
		{
			Name:  "xeon",
			Age:   13,
			Grade: "Grade 6",
			Score: 101.11,
			Email: "xeonjoe@test.com",
			Phone: "16542354654",
		},
	}

	// parameter list  cloud be &list.the item of list cloud be a structure pointer
	err = clientWriter.WriteRows2File(list, true)
	if err != nil {
		panic(err)
	}

}

The result is

name Age Grade mScore mEmail mPhone
Jam 10 Grade 4 99.01 ja***m@test.com 133****6666
xeon 13 Grade 6 101.11 xe***e@test.com 165****4654

Unmarshal a row of a csv to a structure

a csv is

name Age Grade mScore mEmail mPhone
Jam 10 Grade 4 99.01 ja***m@test.com 133****6666
xeon 13 Grade 6 101.11 xe***e@test.com 165****4654
bob 10 Grade 4 99.01 bo***i@test.com 133****6666
package main

import (
	"fmt"
	"github.com/CXeon/easy_csv"
	"os"
)

type testStudentInfo struct {
	Name  string 
	Age   int
	Grade string
	Score float64 
	Email string   
	Phone string  
}

func main() {
	fileName := "./CsvReader.csv"

	csvFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()

	clientReader := easy_csv.NewClientReader(csvFile)

	clientReader.Read() //Important.Exclude title

	data := testStudentInfo{}
	err = clientReader.ReadRowFromFile(&data)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", data)
}

the result show

main.testStudentInfo{Name:"Jam", Age:10, Grade:"Grade 4", Score:99.01, Email:"jam@test.com", Phone:"133*6666"}

If the order of your csv file columns and structure fields is inconsistent

package main

import (
	"fmt"
	"github.com/CXeon/easy_csv"
	"os"
)

type testStudentInfo struct {
	Score float64 
	Email string 
	Phone string  
	Name  string 
	Age   int
	Grade string
}

func main() {
	fileName := "./CsvReader.csv"

	csvFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()

	clientReader := easy_csv.NewClientReader(csvFile)

	clientReader.Read() //Important.Exclude title

	data := testStudentInfo{}

	names := []string{
		"Name",
		"Age",
		"Grade",
		"Score",
		"Email",
		"Phone",
	}
	err = clientReader.ReadRowFromFileWithNames(names, &data)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", data)
}

The result show

main.testStudentInfo{Score:99.01, Email:"jam@test.com", Phone:"133*6666", Name:"Jam", Age:10, Grade:"Grade 4"}

Unmarshal rows of a csv to a structure

a csv is

name Age Grade mScore mEmail mPhone
Jam 10 Grade 4 99.01 ja***m@test.com 133****6666
xeon 13 Grade 6 101.11 xe***e@test.com 165****4654
bob 10 Grade 4 99.01 bo***i@test.com 133****6666
package main

import (
	"fmt"
	"github.com/CXeon/easy_csv"
	"os"
)

type testStudentInfo struct {
	Name  string 
	Age   int
	Grade string
	Score float64 
	Email string  
	Phone string  
}

func main() {
	fileName := "./CsvReader.csv"

	csvFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()

	clientReader := easy_csv.NewClientReader(csvFile)

	clientReader.Read() //Important.Exclude title

	var list []testStudentInfo
	// var list  []*testStudentInfo //yes
	err = clientReader.ReadRowsFromFile(&list)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", list)
}

The result show

[]main.testStudentInfo{main.testStudentInfo{Name:"Jam", Age:10, Grade:"Grade 4", Score:99.01, Email:"jam@test.com", Phone:"1336666"}, main.testStudentInfo{Name:"xeon", Age:13, Grade:"Grade 5", Score:101.11, Email:"xee@test.com", Phone:"1654654"}, main.testStudentInfo{Name:"bob", Age:10, Grade:"Grade 4", Score:99.01, Email:"boi@test.com", Phone:"133***6666"}}

If the order of your csv file columns and structure fields is inconsistent

package main

import (
	"fmt"
	"github.com/CXeon/easy_csv"
	"os"
)

type testStudentInfo struct {
	Score float64
	Email string  
	Phone string  
	Name  string 
	Age   int
	Grade string
}

func main() {
	fileName := "./CsvReader.csv"

	csvFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()

	clientReader := easy_csv.NewClientReader(csvFile)

	clientReader.Read() //Important.Exclude title

	var list []testStudentInfo
	// var list  []*testStudentInfo //yes
	names := []string{
		"Name",
		"Age",
		"Grade",
		"Score",
		"Email",
		"Phone",
	}
	err = clientReader.ReadRowsFromFileWithNames(names, &list)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", list)
}

The result show

[]main.testStudentInfo{main.testStudentInfo{Score:99.01, Email:"jam@test.com", Phone:"1336666", Name:"Jam", Age:10, Grade:"Grade 4"}, main.testStudentInfo{Score:101.11, Email:"xee@test.com", Phone:"1654654", Name:"xeon", Age:13, Grade:"Grade 5"}, main.testStudentInfo{Score:99.01, Email:"boi@test.com", Phone:"133***6666", Name:"bob", Age:10, Grade:"Grade 4"}}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientReader

type ClientReader struct {
	// contains filtered or unexported fields
}

ClientReader a reader client is used to read and unmarshal file of csv

func NewClientReader

func NewClientReader(reader io.Reader, opts ...ClientReaderOptionFunc) *ClientReader

func (*ClientReader) Read

func (reader *ClientReader) Read() ([]string, error)

Read Read one line at a time

func (*ClientReader) ReadAll

func (reader *ClientReader) ReadAll() ([][]string, error)

ReadAll Read all the remaining lines in sequence

func (*ClientReader) ReadRowFromFile

func (reader *ClientReader) ReadRowFromFile(structure interface{}) error

ReadRowFromFile Read a row of lines and parse it into each field of the structure in the order of columns.

structure: The parameter structure is a structure pointer

func (*ClientReader) ReadRowFromFileWithNames

func (reader *ClientReader) ReadRowFromFileWithNames(names []string, structure interface{}) error

ReadRowFromFileWithNames Read a row of lines and parse the data into the corresponding field name of the structure in the order specified by names.

names: The parameter names is field names of structure in order to specify column of file to structure

structure: The parameter structure is a structure pointer

func (*ClientReader) ReadRowsFromFile

func (reader *ClientReader) ReadRowsFromFile(list interface{}) error

ReadRowsFromFile Read rows of remaining lines and parse it into each field of the structure in the order of columns.

list: The parameter list is a list pointer,the item of list must be a structure or a structure pointer

func (*ClientReader) ReadRowsFromFileWithNames

func (reader *ClientReader) ReadRowsFromFileWithNames(names []string, list interface{}) error

ReadRowsFromFileWithNames Read rows of lines and parse the data into the corresponding field name of the structure in the order specified by names.

names: The parameter names is field names of structure in order to specify column of file to structure

list: The parameter list is a list pointer,the item of list must be a structure or a structure pointer

type ClientReaderOption

type ClientReaderOption struct {
	// Comma is the field delimiter.
	// It is set to comma (',') by NewReader.
	// Comma must be a valid rune and must not be \r, \n,
	// or the Unicode replacement character (0xFFFD).
	Comma rune

	// Comment, if not 0, is the comment character. Lines beginning with the
	// Comment character without preceding whitespace are ignored.
	// With leading whitespace the Comment character becomes part of the
	// field, even if TrimLeadingSpace is true.
	// Comment must be a valid rune and must not be \r, \n,
	// or the Unicode replacement character (0xFFFD).
	// It must also not be equal to Comma.
	Comment rune

	// FieldsPerRecord is the number of expected fields per record.
	// If FieldsPerRecord is positive, Read requires each record to
	// have the given number of fields. If FieldsPerRecord is 0, Read sets it to
	// the number of fields in the first record, so that future records must
	// have the same field count. If FieldsPerRecord is negative, no check is
	// made and records may have a variable number of fields.
	FieldsPerRecord int

	// If LazyQuotes is true, a quote may appear in an unquoted field and a
	// non-doubled quote may appear in a quoted field.
	LazyQuotes bool

	// If TrimLeadingSpace is true, leading white space in a field is ignored.
	// This is done even if the field delimiter, Comma, is white space.
	TrimLeadingSpace bool

	// ReuseRecord controls whether calls to Read may return a slice sharing
	// the backing array of the previous call's returned slice for performance.
	// By default, each call to Read returns newly allocated memory owned by the caller.
	ReuseRecord bool
}

type ClientReaderOptionFunc

type ClientReaderOptionFunc func(opt *ClientReaderOption)

func WithReaderComma

func WithReaderComma(comma rune) ClientReaderOptionFunc

func WithReaderComment

func WithReaderComment(comment rune) ClientReaderOptionFunc

func WithReaderFieldsPerRecord

func WithReaderFieldsPerRecord(fieldsPerRecord int) ClientReaderOptionFunc

func WithReaderLazyQuotes

func WithReaderLazyQuotes(lazyQuotes bool) ClientReaderOptionFunc

func WithReuseRecord

func WithReuseRecord(reuse bool) ClientReaderOptionFunc

func WithTrimLeadingSpace

func WithTrimLeadingSpace(trimLeadingSpace bool) ClientReaderOptionFunc

type ClientWriter

type ClientWriter struct {
	// contains filtered or unexported fields
}

ClientWriter a writer client is used to write data to csv

func NewClientWriter

func NewClientWriter(writer io.Writer, opts ...ClientWriterOptionFunc) *ClientWriter

func (*ClientWriter) WriteRow2File

func (writer *ClientWriter) WriteRow2File(structure interface{}, setTitle ...bool) error

WriteRow2File Write a line of data to a file

structure: The parameter data is a structure pointer

func (*ClientWriter) WriteRows2File

func (writer *ClientWriter) WriteRows2File(list interface{}, setTitle ...bool) error

WriteRows2File Write multiple lines of data to a file

list: The parameter list is a list pointer,the item of list must be a structure or a structure pointer

func (*ClientWriter) WriteString2File

func (writer *ClientWriter) WriteString2File(data [][]string) error

WriteString2File 向CSV文件中写入文本数据

data 切片每个元素代表一行,每行元素还是一个切片,其中每个元素代表一列

type ClientWriterOption

type ClientWriterOption struct {
	Comma   rune // Field delimiter (cloud set to ',')
	UseCRLF bool // True to use \r\n as the line terminator
}

type ClientWriterOptionFunc

type ClientWriterOptionFunc func(*ClientWriterOption)

func WithWriterComma

func WithWriterComma(comma rune) ClientWriterOptionFunc

func WithWriterUseCRLF

func WithWriterUseCRLF(useCRLF bool) ClientWriterOptionFunc

Jump to

Keyboard shortcuts

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