GO语言"encoding/csv"包中"Reader"类型的用法及代码示例。
Reader 从CSV-encoded 文件中读取记录。
正如 NewReader 返回的那样,Reader 期望输入符合 RFC 4180。可以更改导出的字段以在第一次调用 Read 或 ReadAll 之前自定义详细信息
Reader 将其输入中的所有 \r\n 序列转换为纯 \n,包括多行字段值,以便返回的数据不依赖于输入文件使用的 line-ending 约定。
用法:
type Reader 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 // Go 1.9
TrailingComma bool // Deprecated: No longer used.
// contains filtered or unexported fields
}
例子:
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"strings"
)
func main() {
in := `first_name,last_name,username
"Rob","Pike",rob
Ken,Thompson,ken
"Robert","Griesemer","gri"
`
r := csv.NewReader(strings.NewReader(in))
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(record)
}
}
输出:
[first_name last_name username] [Rob Pike rob] [Ken Thompson ken] [Robert Griesemer gri]
示例(选项):
此示例显示如何配置 csv.Reader 以处理其他类型的 CSV 文件。
package main
import (
"encoding/csv"
"fmt"
"log"
"strings"
)
func main() {
in := `first_name;last_name;username
"Rob";"Pike";rob
# lines beginning with a # character are ignored
Ken;Thompson;ken
"Robert";"Griesemer";"gri"
`
r := csv.NewReader(strings.NewReader(in))
r.Comma = ';'
r.Comment = '#'
records, err := r.ReadAll()
if err != nil {
log.Fatal(err)
}
fmt.Print(records)
}
输出:
[[first_name last_name username] [Rob Pike rob] [Ken Thompson ken] [Robert Griesemer gri]]
相关用法
- GO Reader.Multistream用法及代码示例
- GO Reader.Len用法及代码示例
- GO Reader用法及代码示例
- GO Reader.ReadAll用法及代码示例
- GO ReadMessage用法及代码示例
- GO Read用法及代码示例
- GO ReadFile用法及代码示例
- GO ReadAtLeast用法及代码示例
- GO ReadDir用法及代码示例
- GO ReadFull用法及代码示例
- GO ReadAll用法及代码示例
- GO Regexp.FindString用法及代码示例
- GO Regexp.FindAllIndex用法及代码示例
- GO ResponseRecorder用法及代码示例
- GO ReverseBytes64用法及代码示例
- GO ReverseBytes16用法及代码示例
- GO Regexp.ReplaceAllLiteralString用法及代码示例
- GO Regexp.FindStringSubmatch用法及代码示例
- GO Regexp.FindAllString用法及代码示例
- GO Regexp.ExpandString用法及代码示例
注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Reader。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。