golagn encoding/cvs 略译[通俗易懂]

golagn encoding/cvs 略译[通俗易懂]GodocViewerDocumentation|Reference|Packages|Commands|TheProject|AboutPackageencoding/csvimport”encoding/csv”

golagn encoding/cvs 略译[通俗易懂]Godoc Viewer

Documentation | Reference | Packages | Commands | The Project | About

Package encoding/csv

import “encoding/csv”

Overview

Index

Overview ? [Top]

Overview ? [Top]

Package csv reads and writes comma-separated values (CSV) files.

A csv file contains zero or more records of one or more fields per record. Each record is separated by the newline character. The final record may optionally be followed by a newline character.

译:

 一个csv文件是一个包含0条或多条记录的文件,每条记录可以有1个或多个域。每条记录用换行符分开。最后一条记录最后可以有换行符。

field1,field2,field3

White space is considered part of a field.

译: 空格符是域的一部分。

Carriage returns before newline characters are silently removed.

译:

Blank lines are ignored. A line with only whitespace characters (excluding the ending newline character) is not considered a blank line.

译: 空行忽略。 如果只有空格符(除了结尾的换行符)不是空行。

Fields which start and stop with the quote character ” are called quoted-fields. The beginning and ending quote are not part of the field.

译: 以双引号开头和结尾的域,称为引号域。 开头和结尾的引号不是域的一部分。

The source:

normal string,”quoted-field”

results in the fields

{`normal string`, `quoted-field`}

Within a quoted-field a quote character followed by a second quote character is considered a single quote.

在一个引号域内,如果后面有第二个引号,则认为是一个引号。

“the “”word”” is true”,”a “”quoted-field”””

results in

{`the “word” is true`, `a “quoted-field”`}

Newlines and commas may be included in a quoted-field

新一行和逗号可以在一个域内。

“Multi-line

field”,”comma is ,”

results in

{`Multi-line

field`, `comma is ,`}

Index ? [Top]

Index ? [Top]

Variables

type ParseError

    func (e *ParseError) Error() string

type Reader

    func NewReader(r io.Reader) *Reader

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

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

type Writer

    func NewWriter(w io.Writer) *Writer

    func (w *Writer) Error() error

    func (w *Writer) Flush()

    func (w *Writer) Write(record []string) (err error)

    func (w *Writer) WriteAll(records [][]string) (err error)

Package files

reader.go writer.go

Variables [Top]

var (

    ErrTrailingComma = errors.New(“extra delimiter at end of line”)

    ErrBareQuote     = errors.New(“bare \” in non-quoted-field”)

    ErrQuote         = errors.New(“extraneous \” in field”)

    ErrFieldCount    = errors.New(“wrong number of fields in line”)

)

These are the errors that can be returned in ParseError.Error

type ParseError [Top]

type ParseError struct {

    Line   int   // Line where the error occurred

    Column int   // Column (rune index) where the error occurred

    Err    error // The actual error

}

A ParseError is returned for parsing errors. The first line is 1. The first column is 0.

func (*ParseError) Error

func (e *ParseError) Error() string

type Reader [Top]

type Reader struct {

    Comma            rune // Field delimiter (set to ‘,’ by NewReader) 域分割符 默认是,

    Comment          rune // Comment character for start of line  注释符在一行的开头

    FieldsPerRecord  int  // Number of expected fields per record 每条记录每个域的开始数字,即从第几个域开始

    LazyQuotes       bool // Allow lazy quotes 允许懒散的引号

    TrailingComma    bool // Allow trailing comma 允许结尾加逗号

    TrimLeadingSpace bool // Trim leading space 开始的空格会去掉

    // contains filtered or unexported fields

}

A Reader reads records from a CSV-encoded file.

As returned by NewReader, a Reader expects input conforming to RFC 4180. The exported fields can be changed to customize the details before the first call to Read or ReadAll.

Comma is the field delimiter. It defaults to ‘,’.

Comment, if not 0, is the comment character. Lines beginning with the Comment character are ignored.

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.

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

If TrailingComma is true, the last field may be an unquoted empty field.

If TrimLeadingSpace is true, leading white space in a field is ignored.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that reads from r.

func (*Reader) Read

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

Read reads one record from r. The record is a slice of strings with each string representing one field.

func (*Reader) ReadAll

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

ReadAll reads all the remaining records from r. Each record is a slice of fields. A successful call returns err == nil, not err == EOF. Because ReadAll is defined to read until EOF, it does not treat end of file as an error to be reported.

type Writer [Top]

type Writer struct {

    Comma   rune // Field delimiter (set to ‘,’ by NewWriter)

    UseCRLF bool // True to use \r\n as the line terminator

    // contains filtered or unexported fields

}

A Writer writes records to a CSV encoded file.

As returned by NewWriter, a Writer writes records terminated by a newline and uses ‘,’ as the field delimiter. The exported fields can be changed to customize the details before the first call to Write or WriteAll.

Comma is the field delimiter.

If UseCRLF is true, the Writer ends each record with \r\n instead of \n.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that writes to w.

func (*Writer) Error

func (w *Writer) Error() error

Error reports any error that has occurred during a previous Write or Flush.

func (*Writer) Flush

func (w *Writer) Flush()

Flush writes any buffered data to the underlying io.Writer. To check if an error occurred during the Flush, call Error.

func (*Writer) Write

func (w *Writer) Write(record []string) (err error)

Writer writes a single CSV record to w along with any necessary quoting. A record is a slice of strings with each string being one field.

func (*Writer) WriteAll

func (w *Writer) WriteAll(records [][]string) (err error)

WriteAll writes multiple CSV records to w using Write and then calls Flush.

今天的文章golagn encoding/cvs 略译[通俗易懂]分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/63979.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注