多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# package csv `import "encoding/csv"` csv读写逗号分隔值(csv)的文件。 一个csv分拣包含零到多条记录,每条记录一到多个字段。每个记录用换行符分隔。最后一条记录后面可以有换行符,也可以没有。 ``` field1,field2,field3 ``` 空白视为字段的一部分。 换行符前面的回车符会悄悄的被删掉。 忽略空行。只有空白的行(除了末尾换行符之外)不视为空行。 以双引号"开始和结束的字段成为受引字段,其开始和结束的引号不属于字段。 资源: ``` normal string,"quoted-field" ``` 产生如下字段: ``` {`normal string`, `quoted-field`} ``` 受引字段内部,如果有两个连续的双引号,则视为一个单纯的双引号字符: ``` "the ""word"" is true","a ""quoted-field""" ``` 生成: ``` {`the "word" is true`, `a "quoted-field"`} ``` 受引字段里可以包含换行和逗号: ``` "Multi-line field","comma is ," ``` 生成: ``` {`Multi-line field`, `comma is ,`} ``` ## Index * [Variables](#pkg-variables) * [type ParseError](#ParseError) * [func (e \*ParseError) Error() string](#ParseError.Error) * [type Reader](#Reader) * [func NewReader(r io.Reader) \*Reader](#NewReader) * [func (r \*Reader) Read() (record []string, err error)](#Reader.Read) * [func (r \*Reader) ReadAll() (records [][]string, err error)](#Reader.ReadAll) * [type Writer](#Writer) * [func NewWriter(w io.Writer) \*Writer](#NewWriter) * [func (w \*Writer) Write(record []string) (err error)](#Writer.Write) * [func (w \*Writer) WriteAll(records [][]string) (err error)](#Writer.WriteAll) * [func (w \*Writer) Flush()](#Writer.Flush) * [func (w \*Writer) Error() error](#Writer.Error) ## Variables ``` 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") ) ``` 这些都是PaserError.Err字段可能的值。 ## type [ParseError](https://github.com/golang/go/blob/master/src/encoding/csv/reader.go#L63 "View Source") ``` type ParseError struct { Line int // 出错的行号 Column int // 出错的列号 Err error // 具体的错误 } ``` 当解析错误时返回ParseError,第一个行为1,第一列为0。 ### func (\*ParseError) [Error](https://github.com/golang/go/blob/master/src/encoding/csv/reader.go#L69 "View Source") ``` func (e *ParseError) Error() string ``` ## type [Reader](https://github.com/golang/go/blob/master/src/encoding/csv/reader.go#L102 "View Source") ``` type Reader struct { Comma rune // 字段分隔符(NewReader将之设为',') Comment rune // 一行开始位置的注释标识符 FieldsPerRecord int // 每条记录期望的字段数 LazyQuotes bool // 允许懒引号 TrailingComma bool // 忽略,出于后端兼容性而保留 TrimLeadingSpace bool // 去除前导的空白 // 内含隐藏或非导出字段 } ``` Reader从csv编码的文件中读取记录。 NewReader返回的*Reader期望输入符合[RFC 4180](http://tools.ietf.org/html/rfc4180)。在首次调用Read或者ReadAll之前可以设定导出字段的细节。 Comma是字段分隔符,默认为','。Comment如果不是0,则表示注释标识符,以Comment开始的行会被忽略。如果FieldsPerRecord大于0,Read方法要求每条记录都有给定数目的字段。如果FieldsPerRecord等于0,Read方法会将其设为第一条记录的字段数,因此其余的记录必须有同样数目的字段。如果FieldsPerRecord小于0,不会检查字段数,允许记录有不同数量的字段。如果LazyQuotes为真,引号可以出现在非受引字段里,不连续两个的引号可以出现在受引字段里。如果TrimLeadingSpace为真,字段前导的空白会忽略掉。 ### func [NewReader](https://github.com/golang/go/blob/master/src/encoding/csv/reader.go#L116 "View Source") ``` func NewReader(r io.Reader) *Reader ``` NewReader函数返回一个从r读取的*Reader。 ### func (\*Reader) [Read](https://github.com/golang/go/blob/master/src/encoding/csv/reader.go#L134 "View Source") ``` func (r *Reader) Read() (record []string, err error) ``` Read从r读取一条记录,返回值record是字符串的切片,每个字符串代表一个字段。 ### func (\*Reader) [ReadAll](https://github.com/golang/go/blob/master/src/encoding/csv/reader.go#L161 "View Source") ``` func (r *Reader) ReadAll() (records [][]string, err error) ``` ReadAll从r中读取所有剩余的记录,每个记录都是字段的切片,成功的调用返回值err为nil而不是EOF。因为ReadAll方法定义为读取直到文件结尾,因此它不会将文件结尾视为应该报告的错误。 ## type [Writer](https://github.com/golang/go/blob/master/src/encoding/csv/writer.go#L24 "View Source") ``` type Writer struct { Comma rune // 字段分隔符(NewWriter将之设为',') UseCRLF bool // 如为真,则换行时使用\r\n // 内含隐藏或非导出字段 } ``` Writer类型的值将记录写入一个csv编码的文件。 NewWriter返回的*Writer写入记录时,以换行结束记录,用','分隔字段。在第一次调用Write或WriteAll之前,可以设置导出字段的细节。 Comma是字段分隔符。如果UseCRLF为真,Writer在每条记录结束时用\r\n代替\n。 ### func [NewWriter](https://github.com/golang/go/blob/master/src/encoding/csv/writer.go#L31 "View Source") ``` func NewWriter(w io.Writer) *Writer ``` NewWriter返回一个写入w的*Writer。 ### func (\*Writer) [Write](https://github.com/golang/go/blob/master/src/encoding/csv/writer.go#L40 "View Source") ``` func (w *Writer) Write(record []string) (err error) ``` 向w中写入一条记录,会自行添加必需的引号。记录是字符串切片,每个字符串代表一个字段。 ### func (\*Writer) [WriteAll](https://github.com/golang/go/blob/master/src/encoding/csv/writer.go#L107 "View Source") ``` func (w *Writer) WriteAll(records [][]string) (err error) ``` WriteAll方法使用Write方法向w写入多条记录,并在最后调用Flush方法清空缓存。 ### func (\*Writer) [Flush](https://github.com/golang/go/blob/master/src/encoding/csv/writer.go#L96 "View Source") ``` func (w *Writer) Flush() ``` 将缓存中的数据写入底层的io.Writer。要检查Flush时是否发生错误的话,应调用Error方法。 ### func (\*Writer) [Error](https://github.com/golang/go/blob/master/src/encoding/csv/writer.go#L101 "View Source") ``` func (w *Writer) Error() error ``` Error返回在之前的Write方法和Flush方法执行时出现的任何错误。