亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

directory search
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫/sql) database/sql/driver(數(shù)據(jù)庫/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語法樹) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達(dá)式) regexp/syntax runtime runtime(運行時) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競爭) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
characters

  • import "encoding/csv"

  • Overview

  • Index

  • Examples

概觀

包csv讀取和寫入逗號分隔值(CSV) 文件。CSV文件有很多種,該軟件包支持RFC 4180中描述的格式。

一個csv文件包含每個記錄一個或多個字段的零個或多個記錄。每條記錄由換行符分隔。最后的記錄可以選擇跟隨一個換行符。

field1,field2,field3

白色空間被視為一個領(lǐng)域的一部分。

在換行符被無聲刪除之前,回車返回。

空白行被忽略。只有空白字符的行(不包括結(jié)尾換行符)不被視為空白行。

以引號字符開頭和結(jié)尾的字段稱為引用字段。開始和結(jié)束引號不是字段的一部分。

來源:

normal string,"quoted-field"

結(jié)果在這些領(lǐng)域

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

在引用字段中,引號字符后跟第二個引號字符被視為單引號。

"the ""word"" is true","a ""quoted-field"""

results in

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

換行符和逗號可以包含在引用字段中

"Multi-line
field","comma is ,"

results in

{`Multi-line
field`, `comma is ,`}

指數(shù)

  • 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) error

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

例子

Reader Reader.ReadAll Reader(選項)Writer Writer.WriteAll

包文件

reader.go writer.go

變量

這些是可以在ParseError.Error中返回的錯誤

var (
        ErrTrailingComma = errors.New("extra delimiter at end of line") // no longer used
        ErrBareQuote     = errors.New("bare \" in non-quoted-field")
        ErrQuote         = errors.New("extraneous \" in field")
        ErrFieldCount    = errors.New("wrong number of fields in line"))

type ParseError

解析錯誤返回ParseError。第一行是1.第一列是0。

type ParseError struct {
        Line   int   // Line where the error occurred
        Column int   // Column (rune index) where the error occurred
        Err    error // The actual error}

func (*ParseError) Error

func (e *ParseError) Error() string

type Reader

Reader從CSV編碼的文件中讀取記錄。

正如NewReader返回的那樣,Reader期望符合RFC 4180的輸入??梢栽诘谝淮握{(diào)用Read或ReadAll之前更改導(dǎo)出的字段以定制細(xì)節(jié)。

type Reader struct {        // Comma is the field delimiter.        // It is set to comma (',') by NewReader.
        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 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
        TrailingComma bool // ignored; here for backwards compatibility        // 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        // contains filtered or unexported fields}

package mainimport ("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)}}

示例(選項)

此示例顯示如何配置csv.Reader以處理其他類型的CSV文件。

package mainimport ("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)}

func NewReader

func NewReader(r io.Reader) *Reader

NewReader返回一個新的從r讀取的Reader。

func (*Reader) Read

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

讀取從r讀取一個記錄(一段字段)。如果記錄具有意外數(shù)量的字段,則Read將返回記錄以及錯誤ErrFieldCount。除此之外,Read總是返回一個非零記錄或一個非零錯誤,但不是兩者都有。如果沒有數(shù)據(jù)需要讀取,則讀取返回nil,io.EOF。如果ReuseRecord為true,則可以在多次調(diào)用Read之間共享返回的切片。

func (*Reader) ReadAll

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

ReadAll從r讀取所有剩余的記錄。每條記錄都是一片田地。成功的調(diào)用返回err == nil,而不是err == io.EOF。由于ReadAll被定義為讀取直到EOF,因此它不會將文件末尾視為要報告的錯誤。

package mainimport ("encoding/csv""fmt""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))

	records, err := r.ReadAll()if err != nil {
		log.Fatal(err)}

	fmt.Print(records)}

type Writer

Writer將記錄寫入CSV編碼文件。

如NewWriter返回的,Writer寫入由換行符終止的記錄,并使用','作為字段分隔符。在首次調(diào)用Write或WriteAll之前,可以更改導(dǎo)出的字段以定制細(xì)節(jié)。

逗號是字段分隔符。

如果UseCRLF為true,則Writer以\ r \ n結(jié)束每個記錄而不是\ n。

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}

package mainimport ("encoding/csv""log""os")func main() {
	records := [][]string{{"first_name", "last_name", "username"},{"Rob", "Pike", "rob"},{"Ken", "Thompson", "ken"},{"Robert", "Griesemer", "gri"},}

	w := csv.NewWriter(os.Stdout)for _, record := range records {if err := w.Write(record); err != nil {
			log.Fatalln("error writing record to csv:", err)}}// Write any buffered data to the underlying writer (standard output).
	w.Flush()if err := w.Error(); err != nil {
		log.Fatal(err)}}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter返回一個寫入w的新Writer。

func (*Writer) Error

func (w *Writer) Error() error

錯誤報告在先前寫入或刷新期間發(fā)生的任何錯誤。

func (*Writer) Flush

func (w *Writer) Flush()

Flush將任何緩沖的數(shù)據(jù)寫入底層的io.Writer。要檢查在刷新過程中是否發(fā)生錯誤,請調(diào)用錯誤。

func (*Writer) Write

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

Writer寫一個CSV記錄以及任何必要的報價。記錄是一串字符串,每個字符串都是一個字段。

func (*Writer) WriteAll

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

WriteAll使用Write寫入多個CSV記錄,然后調(diào)用Flush。

package mainimport ("encoding/csv""log""os")func main() {
	records := [][]string{{"first_name", "last_name", "username"},{"Rob", "Pike", "rob"},{"Ken", "Thompson", "ken"},{"Robert", "Griesemer", "gri"},}

	w := csv.NewWriter(os.Stdout)
	w.WriteAll(records) // calls Flush internallyif err := w.Error(); err != nil {
		log.Fatalln("error writing csv:", err)}}
Previous article: Next article: