goCsv

package module
v0.0.0-...-95221cf Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2018 License: Apache-2.0 Imports: 4 Imported by: 2

README

go-csv

实现类似Python的csv.DictReader和csv.DictWriter

Example

package main

import (
	"fmt"
	"io"
	"os"

	"github.com/ibbd-dev/go-csv"
)

func main() {
	fname := "./test.csv"
	f, err := os.Open(fname)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	reader := goCsv.NewMapReader(f)
	fieldnames, err := reader.GetFieldnames()
	if err != nil {
		panic(err)
	}

	outFilename := "./out.csv"
	wf, err := os.Create(outFilename)
	if err != nil {
		panic(err)
	}
	defer wf.Close()
	writer := goCsv.NewMapWriter(wf, fieldnames)
	writer.WriteHeader()

	for {
		row, err := reader.Read()
		if err == io.EOF {
			fmt.Printf("the file is over")
			break
		}
		if err != nil {
			panic(err)
		}
		fmt.Printf("Row 1: %+v\n", row)

		if err = writer.WriteRow(row); err != nil {
			panic(err)
		}
	}

	writer.Flush()  // 注意最后需要刷到磁盘
}

MapReader API

func NewMapReader(r io.Reader) *MapReader

// Init 配置csv的基本参数
func (r *MapReader) Init(params csv.Reader)

// SetSkip 设置跳过前面的若干条记录
func (r *MapReader) SetSkip(skip int) 

// SetLimit 设置只提取若干条记录
func (r *MapReader) SetLimit(limit int) 

// SetFieldnames 指定csv文件的字段名
// 如果不指定的话,则默认使用csv文件的第一行作为字段名
func (r *MapReader) SetFieldnames(fieldnames []string)

func (r *MapReader) GetFieldnames() (fieldnames []string, err error) 

// Read 读取一行记录
func (r *MapReader) Read() (record map[string]string, err error) 

// ReadAll 读取全部的内容
func (r *MapReader) ReadAll() (records []map[string]string, err error)

MapWriter API

func NewMapWriter(w io.Writer, fieldnames []string) *MapWriter 

// NewMapWriterSimple 简化的writer对象,可以通过SetHeader方法来设置Header
func NewMapWriterSimple(w io.Writer) *MapWriter 

func (w *MapWriter) SetHeader(fieldnames []string) 

// SetFieldNotSetErr 字段未设置时,是否报错
// 默认为false,即当字段为设置时,会自动使用空字符串补充
func (w *MapWriter) SetFieldNotSetErr(fieldNotSetErr bool) 

// Init 配置csv的基本参数
func (w *MapWriter) Init(params csv.Writer) 

func (w *MapWriter) WriteHeader() (err error) 

func (w *MapWriter) WriteRow(row map[string]string) (err error) 

func (w *MapWriter) WriteRows(rows []map[string]string) (err error) 

// Flush 将数据刷到磁盘
func (w *MapWriter) Flush() 

Reader API

func NewReader(r io.Reader) *Reader

// Init 配置csv的基本参数
func (r *Reader) Init(params csv.Reader)

// SetSkip 设置跳过前面的若干条记录
func (r *Reader) SetSkip(skip int) 

// SetLimit 设置只提取若干条记录
func (r *Reader) SetLimit(limit int) 

func (r *Reader) GetFieldnames() (fieldnames []string, err error) 

// Read 读取一行记录
func (r *Reader) Read() (record []string, err error) 

// ReadAll 读取全部的内容
func (r *Reader) ReadAll() (records [][]string, err error)

utils

// CountLines 统计csv文件的记录数
func CountLines(r io.Reader) (n int, err error)

Documentation

Overview

参考Python的DictReader

参考Python的DictReader

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountLines

func CountLines(r io.Reader) (n int, err error)

CountLines 统计csv文件的记录数

Types

type MapReader

type MapReader struct {
	Reader
}

func NewMapReader

func NewMapReader(r io.Reader) *MapReader

func (*MapReader) Read

func (r *MapReader) Read() (record map[string]string, err error)

Read 读取一行记录

func (*MapReader) ReadAll

func (r *MapReader) ReadAll() (records []map[string]string, err error)

ReadAll 读取全部的内容

type MapWriter

type MapWriter struct {
	Writer *csv.Writer
	// contains filtered or unexported fields
}

func NewMapWriter

func NewMapWriter(w io.Writer, fieldnames []string) *MapWriter

func NewMapWriterSimple

func NewMapWriterSimple(w io.Writer) *MapWriter

NewMapWriterSimple 简化的writer对象,可以通过SetHeader方法来设置Header

func (*MapWriter) Flush

func (w *MapWriter) Flush()

Flush 将数据刷到磁盘

func (*MapWriter) Init

func (w *MapWriter) Init(params csv.Writer)

Init 配置csv的基本参数

func (*MapWriter) SetFieldNotSetErr

func (w *MapWriter) SetFieldNotSetErr(fieldNotSetErr bool)

SetFieldNotSetErr 字段未设置时,是否报错 默认为false,即当字段为设置时,会自动使用空字符串补充

func (*MapWriter) SetHeader

func (w *MapWriter) SetHeader(fieldnames []string)

func (*MapWriter) WriteHeader

func (w *MapWriter) WriteHeader() (err error)

func (*MapWriter) WriteRow

func (w *MapWriter) WriteRow(row map[string]string) (err error)

func (*MapWriter) WriteRows

func (w *MapWriter) WriteRows(rows []map[string]string) (err error)

type Reader

type Reader struct {
	Reader *csv.Reader
	// contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) GetFieldnames

func (r *Reader) GetFieldnames() (fieldnames []string, err error)

GetFieldnames 获取csv文件的header csv文件在处理的时候,可能会最后会出现空字段 Error: Multiple indices with the same name ”

func (*Reader) Init

func (r *Reader) Init(params csv.Reader)

Init 配置csv的基本参数

func (*Reader) Read

func (r *Reader) Read() (record []string, err error)

Read 读取一行记录

func (*Reader) ReadAll

func (r *Reader) ReadAll() (records [][]string, err error)

ReadAll 读取全部的内容

func (*Reader) SetFieldnames

func (r *Reader) SetFieldnames(fieldnames []string)

SetFieldnames 指定csv文件的字段名 如果不指定的话,则默认使用csv文件的第一行作为字段名

func (*Reader) SetLimit

func (r *Reader) SetLimit(limit int)

SetLimit 设置只提取若干条记录

func (*Reader) SetSkip

func (r *Reader) SetSkip(skip int)

SetSkip 设置跳过前面的若干条记录

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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