本文整理汇总了Golang中encoding/csv.Reader类的典型用法代码### 示例。如果您正苦于以下问题:Golang Reader类的具体用法?Golang Reader怎么用?Golang Reader使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了Reader类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: LoadMicrophones

func (mfr *MicrophoneFileReader) LoadMicrophones(reader *csv.Reader) (bool, error) {
    records, err := reader.ReadAll()

    if err != nil {
        fmt.Println(err)
        return false, err
    }

    for i := 0; i < len(records); i++ {
        price, err := strconv.ParseFloat(records[i][3], 64)

        if err != nil {
            return false, errors.New("Not able to parse price to float")
        }

        mic := Microphone{
            name:        records[i][0],
            brand:       records[i][1],
            description: records[i][2],
            price:       price,
            url:         records[i][4],
            micType:     records[i][5],
            micStyle:    records[i][6],
        }
        mfr.microphoneList = append(mfr.microphoneList, mic)
    }

    return true, nil
}

开发者ID:settermjd,项目名称:microphones,代码行数:29,代码来源:csv.go

示例2: Read

// Read takes a CSV reader and reads it into a typed List of structs. Each row gets read into a struct named structName, described by headers. If the original data contained headers it is expected that the input reader has already read those and are pointing at the first data row.
// If kinds is non-empty, it will be used to type the fields in the generated structs; otherwise, they will be left as string-fields.
// In addition to the list, Read returns the typeRef for the structs in the list, and last the typeDef of the structs.
func Read(r *csv.Reader, structName string, headers []string, kinds KindSlice, vrw types.ValueReadWriter) (l types.List, typeRef, typeDef types.Type) {
    typeRef, typeDef = MakeStructTypeFromHeaders(headers, structName, kinds)
    valueChan := make(chan types.Value, 128) // TODO: Make this a function param?
    listType := types.MakeCompoundType(types.ListKind, typeRef)
    listChan := types.NewStreamingTypedList(listType, vrw, valueChan)

    structFields := typeDef.Desc.(types.StructDesc).Fields

    for {
        row, err := r.Read()
        if err == io.EOF {
            close(valueChan)
            break
        } else if err != nil {
            panic(err)
        }

        fields := make(map[string]types.Value)
        for i, v := range row {
            if i < len(headers) {
                f := structFields[i]
                fields[f.Name] = StringToType(v, f.T.Kind())
            }
        }
        valueChan <- types.NewStruct(typeRef, typeDef, fields)
    }

    return <-listChan, typeRef, typeDef
}

开发者ID:arv,项目名称:noms-old,代码行数:32,代码来源:read.go

示例3: streamCsv

// streamCsv
//  Streams a CSV Reader into a returned channel.  Each CSV row is streamed along with the header.
//  "true" is sent to the `done` channel when the file is finished.
//
// Args
//  csv    - The csv.Reader that will be read from.
//  buffer - The "lines" buffer factor.  Send "0" for an unbuffered channel.
func streamCsv(csv *csv.Reader, buffer int) (lines chan *CsvLine) {
    lines = make(chan *CsvLine, buffer)

    go func() {
        // get Header
        header, err := csv.Read()
        if err != nil {
            close(lines)
            return
        }

        i := 0

        for {
            line, err := csv.Read()

            if len(line) > 0 {
                i++
                lines <- &CsvLine{Header: header, Line: line}
            }

            if err != nil {
                fmt.Printf("Sent %d lines\n", i)
                close(lines)
                return
            }
        }
    }()

    return
}

开发者ID:jkamenik,项目名称:go_file_parser,代码行数:38,代码来源:csv_streamer.go

示例4: unmarshalCensusData

// Reads the census CSV data from files
func unmarshalCensusData(reader *csv.Reader, v interface{}) error {
    record, err := reader.Read()
    if err != nil {
        return err
    }
    s := reflect.ValueOf(v).Elem()
    if s.NumField() != len(record) {
        return &csvFieldMismatch{s.NumField(), len(record)}
    }
    for i := 0; i < s.NumField(); i++ {
        f := s.Field(i)
        switch f.Type().String() {
        case "string":
            f.SetString(record[i])
        case "int":
            ival, err := strconv.ParseInt(record[i], 10, 0)
            if err != nil {
                return err
            }
            f.SetInt(ival)
        case "float64":
            fval, err := strconv.ParseFloat(record[i], 64)
            if err != nil {
                return err
            }
            f.SetFloat(fval)
        default:
            return &csvUnsupportedType{f.Type().String()}
        }
    }
    return nil
}

开发者ID:RavenB,项目名称:harvester,代码行数:33,代码来源:util.go

示例5: Get

// Get is to get OTC csv data.
func (o *OTCLists) Get(category string) ([][]string, error) {
    var (
        csvArrayContent []string
        csvReader       *csv.Reader
        data            []byte
        err             error
        rawData         [][]string
        url             string
    )

    url = fmt.Sprintf("%s%s", utils.OTCHOST, fmt.Sprintf(utils.OTCLISTCSV, fmt.Sprintf("%d/%02d/%02d", o.Date.Year()-1911, o.Date.Month(), o.Date.Day()), category))

    if data, err = hCache.Get(url, false); err == nil {
        csvArrayContent = strings.Split(string(data), "\n")
        if len(csvArrayContent) > 5 {
            csvReader = csv.NewReader(strings.NewReader(strings.Join(csvArrayContent[4:len(csvArrayContent)-1], "\n")))
            if rawData, err = csvReader.ReadAll(); err == nil {
                o.categoryRawData[category] = rawData
                o.formatData(category)
                return rawData, nil
            }
        }
    }
    return nil, err
}

开发者ID:668Jerry,项目名称:gogrs,代码行数:26,代码来源:twselist.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng