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

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

示例1: writeCsv

func writeCsv(ch chan []string) {
    var writer *csv.Writer
    if outputFn == "" {
        writer = csv.NewWriter(os.Stdout)
    } else {
        file, err := os.Create(outputFn)
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(1)
        }
        defer file.Close()
        writer = csv.NewWriter(file)
    }

    r, _ := utf8.DecodeRuneInString(outputSep)
    writer.Comma = r

    for row := range ch {
        err := writer.Write(row)
        if err != nil {
            fmt.Println("Error:", err)
            close(ch)
            return
        }
    }
    writer.Flush()
}

开发者ID:hlawrenz,项目名称:csvmung,代码行数:27,代码来源:csvmung.go

示例2: main

func main() {
    flag.Parse()

    if *showVersion {
        fmt.Printf("json2csv v1.1\n")
        return
    }

    var reader *bufio.Reader
    var writer *csv.Writer
    if *inputFile != "" {
        file, err := os.OpenFile(*inputFile, os.O_RDONLY, 0600)
        if err != nil {
            log.Printf("Error %s opening %v", err, *inputFile)
            return
        }
        reader = bufio.NewReader(file)
    } else {
        reader = bufio.NewReader(os.Stdin)
    }

    if *outputFile != "" {
        file, err := os.OpenFile(*outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
        if err != nil {
            log.Printf("Error %s opening outputFile %v", err, *outputFile)
        }
        writer = csv.NewWriter(file)
    } else {
        writer = csv.NewWriter(os.Stdout)
    }

    json2csv(reader, writer, keys)
}

开发者ID:SeanOC,项目名称:json2csv,代码行数:33,代码来源:main.go

示例3: zipOutput1

func zipOutput1(username string, wholeList, responce map[string]string, undesireables []map[string]string) {
    undesireableNames := []string{"Unsubscribes", "Bounce", "Invalids", "Blocks", "Spam Reports"}
    wholelistname := username + "DONOTSEND.csv"
    donotsendname := username + "newlist.csv"

    outputFile, _ := os.Create(wholelistname)
    defer outputFile.Close()
    csvOutput := csv.NewWriter(outputFile)

    for i := 0; i < len(undesireables); i++ {
        csvOutput.Write([]string{undesireableNames[i]})
        for k, _ := range undesireables[i] {
            csvOutput.Write([]string{k})
            csvOutput.Flush()
        }
        csvOutput.Write([]string{})
    }

    newlistOutputFile, _ := os.Create(donotsendname)
    defer newlistOutputFile.Close()

    newlistCsvOutput := csv.NewWriter(newlistOutputFile)
    for k, _ := range wholeList {
        newlistCsvOutput.Write([]string{k})
        newlistCsvOutput.Flush()
    }

    // Create a buffer to write our archive to.
    buf := new(bytes.Buffer)

    // Create a new zip archive.
    w := zip.NewWriter(buf)

    // Add some files to the archive.
    var files = []struct {
        Name, Body string
    }{
        {wholelistname, "This archive contains your new list."},
        {donotsendname, "This archive contains the emails that should not be sent to again."},
    }
    for _, file := range files {
        f, err := w.Create(file.Name)
        if err != nil {
            log.Fatal(err)
        }
        _, err = f.Write([]byte(file.Body))
        if err != nil {
            log.Fatal(err)
        }
    }

    // Make sure to check the error on Close.
    err := w.Close()
    if err != nil {
        log.Fatal(err)
    }
    //write the zipped file to the disk
    ioutil.WriteFile(username+"_.zip", buf.Bytes(), 0777)
}

开发者ID:kernkw,项目名称:upload-files-go,代码行数:59,代码来源:update.go

示例4: processCompact

func processCompact(sess rets.Requester, ctx context.Context, req rets.SearchRequest, output *string) {
    w := csv.NewWriter(os.Stdout)
    if *output != "" {
        os.MkdirAll(*output, 0777)
        f, _ := os.Create(*output + "/results.csv")
        defer f.Close()
        w = csv.NewWriter(f)
    }
    defer w.Flush()

    // loop over all the pages we need
    for {
        fmt.Printf("Querying next page: %v\n", req)
        result, err := rets.SearchCompact(sess, ctx, req)
        if err != nil {
            panic(err)
        }
        switch result.Response.Code {
        case rets.StatusOK:
            // we got some daters
        case rets.StatusNoRecords:
            return
        case rets.StatusSearchError:
            fallthrough
        default: // shit hit the fan
            panic(errors.New(result.Response.Text))
        }
        count := 0
        if count == 0 {
            w.Write(result.Columns)
        }
        hasMoreRows, err := result.ForEach(func(row rets.Row, err error) error {
            if err != nil {
                return err
            }
            w.Write(row)
            count++
            return err
        })
        result.Close()
        if err != nil {
            panic(err)
        }
        if !hasMoreRows {
            return
        }
        if req.Offset == 0 {
            req.Offset = 1
        }
        req.Offset = req.Offset + count
    }
}

开发者ID:jpfielding,项目名称:gorets,代码行数:52,代码来源:example.go

示例5: DataWriter

func DataWriter(output *string) *csv.Writer {
    if *output == "stdout" {
        return csv.NewWriter(os.Stdout)
    } else {
        outfile, err := os.Create(*output)

        if err != nil {
            fmt.Println(err)
            panic(err)
        }

        return csv.NewWriter(outfile)
    }
}

开发者ID:ramin,项目名称:hashpipe,代码行数:14,代码来源:main.go

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