當前位置: 首頁>>代碼示例>>Golang>>正文


Golang csv.NewWriter函數代碼示例

本文整理匯總了Golang中encoding/csv.NewWriter函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewWriter函數的具體用法?Golang NewWriter怎麽用?Golang NewWriter使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewWriter函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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

示例6: RunCommitters

// RunCommitters lists stargazers by commits to subscribed repos, from
// most prolific committer to least.
func RunCommitters(c *fetch.Context, sg []*fetch.Stargazer, rs map[string]*fetch.Repo) error {
	log.Infof("running committers analysis")

	// Open file and prepare.
	f, err := createFile(c, "committers.csv")
	if err != nil {
		return util.Errorf("failed to create file: %s", err)
	}
	defer f.Close()
	w := csv.NewWriter(f)
	if err := w.Write([]string{"Login", "Email", "Commits", "Additions", "Deletions"}); err != nil {
		return util.Errorf("failed to write to CSV: %s", err)
	}

	// Sort the stargazers.
	slice := Contributors(sg)
	sort.Sort(slice)

	// Now accumulate by days.
	for _, s := range slice {
		c, a, d := s.TotalCommits()
		if c == 0 {
			break
		}
		if err := w.Write([]string{s.Login, s.Email, strconv.Itoa(c), strconv.Itoa(a), strconv.Itoa(d)}); err != nil {
			return util.Errorf("failed to write to CSV: %s", err)
		}
	}
	w.Flush()
	log.Infof("wrote committers analysis to %s", f.Name())

	return nil
}
開發者ID:spencerkimball,項目名稱:stargazers,代碼行數:35,代碼來源:analyze.go

示例7: newDroid

func newDroid(w io.Writer) *droidWriter {
	return &droidWriter{
		parents: make(map[string]parent),
		rec:     make([]string, 18),
		w:       csv.NewWriter(os.Stdout),
	}
}
開發者ID:glepore70,項目名稱:siegfried,代碼行數:7,代碼來源:writers.go

示例8: ExportHandler

func ExportHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		language := r.FormValue("language")
		fmt.Println("Exporting in", language)
		translations := model.GetPreferredTranslations(language)

		w.Header().Set("Content-Encoding", "UTF-8")
		w.Header().Set("Content-Type", "application/csv; charset=UTF-8")
		w.Header().Set("Content-Disposition", "attachment; filename=\""+model.LanguageNames[language]+".csv\"")

		out := csv.NewWriter(w)
		out.Write([]string{
			"Original",
			"Part of",
			"Translation",
		})
		for _, translation := range translations {
			out.Write([]string{
				translation.Entry.Original,
				translation.Entry.PartOf,
				translation.Translation,
			})
		}
		out.Flush()
		return
	} else {
		renderTemplate("export", w, r, func(data TemplateData) TemplateData {
			return data
		})
	}
}
開發者ID:knight73geo,項目名稱:charactersheets,代碼行數:31,代碼來源:translate.go

示例9: New

// New writes all of the Rows in a Table to a CSV file.
func New(source optimus.Table, filename string) error {
	fout, err := os.Create(filename)
	defer fout.Close()
	if err != nil {
		return err
	}
	writer := csv.NewWriter(fout)
	headers := []string{}
	wroteHeader := false
	for row := range source.Rows() {
		if !wroteHeader {
			headers = convertRowToHeader(row)
			if err := writer.Write(headers); err != nil {
				return err
			}
			wroteHeader = true
		}
		if err := writer.Write(convertRowToRecord(row, headers)); err != nil {
			return err
		}
	}
	if source.Err() != nil {
		return source.Err()
	}
	writer.Flush()
	if writer.Error() != nil {
		return writer.Error()
	}
	return nil
}
開發者ID:nathanleiby,項目名稱:optimus,代碼行數:31,代碼來源:csv.go

示例10: scrapEvents

// Scrap events for the specified lowerEventID (inclusive) to
// upperEventID (exclusive). Writes scraped events to the specified fileName as CSV.
func scrapEvents(lowerEventID int, upperEventID int, fileName string) {
	delta := upperEventID - lowerEventID
	if delta < 0 {
		panic("lowerEventID (" + string(lowerEventID) +
			") is less than upperEventID (" + string(upperEventID) + ")")
	}

	csvFile, err := os.Create(fileName)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()
	writer := csv.NewWriter(csvFile)

	writtenEvents := 0
	for i := 0; i < delta; i++ {
		scrapedEvent := scrapEvent(lowerEventID + i)
		if scrapedEvent != nil {
			writer.Write(scrapedEvent)
			writtenEvents++
		}
	}
	writer.Flush()

	fmt.Println("number of scraped events written: ", writtenEvents)
}
開發者ID:jamesma,項目名稱:html-scraper,代碼行數:28,代碼來源:chamberorganizer.go

示例11: prepareCSV

func prepareCSV(ctx *Context) error {

	// Collect all the data we need from the database.
	log.Println("Retrieving records...")
	rows, err := ctx.In.Connection.Query("SELECT token, vendor, app_id, language, user_info FROM devices")
	if err != nil {
		return err
	}
	defer rows.Close()

	// Open the output file.
	log.Printf("Opening %s for output...", ctx.Out.CSVFile)
	file, err := os.OpenFile(ctx.Out.CSVFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		return err
	}

	// Create a CSV writer and dump the records in it.
	writer := csv.NewWriter(file)
	log.Println("Exporting records...")
	for rows.Next() {
		d := NewDevice()
		rows.Scan(&d.Token, &d.Vendor, &d.AppID, &d.Language, &d.UserInfo)
		if err := writer.Write(ctx.Exporter.ToCSV(d)); err != nil {
			return err
		}
	}

	writer.Flush()
	return nil
}
開發者ID:contactlab,項目名稱:CLABPush-Go,代碼行數:31,代碼來源:main.go

示例12: AnalyzeAllResponses

// Analyze all significant dates with a histogram. But also write any significant data
// to a csv file so we can do further processing in R
func AnalyzeAllResponses(a *Analyzer, ar []*Response) {
	f, err := os.Create("swing-data.csv")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	w := csv.NewWriter(f)
	defer w.Flush()
	Range := histogram.Range(-1.0, 200, .01)
	h, err := histogram.NewHistogram(Range)
	if err != nil {
		panic(err)
	}
	for _, resp := range ar {
		for _, oi := range a.AnalyzeStock(resp) {
			var toWrite = []string{
				strconv.FormatFloat(oi.Swing, 'f', 4, 64),
				strconv.FormatFloat(oi.Ret, 'f', 4, 64),
			}
			w.Write(toWrite)
			h.Add(oi.Ret)
		}
	}
	fmt.Println("MEAN: ", h.Mean())
	fmt.Println("SIGMA ", h.Sigma())
}
開發者ID:hahnicity,項目名稱:hypecheck,代碼行數:28,代碼來源:analyze.go

示例13: SaveDebtData

func SaveDebtData(l []DebtItem, filename string) error {
	csvfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0660)
	if err != nil {
		if _, err := os.Stat(filename); err != nil {
			csvfile, _ = os.Create(filename)
		} else {
			fmt.Printf("Error opening debt file: %v", err)
			panic(err)
		}
	}
	defer csvfile.Close()
	writer := csv.NewWriter(csvfile)
	for _, item := range l {
		err = writer.Write([]string{item.Debtor.Name,
			item.Creditor.Name,
			fmt.Sprintf("%d", item.Amount.Cents),
			item.Note,
			strconv.Itoa(int(item.Creation.Unix()))})
		if err != nil {
			fmt.Println(err)
		}
	}
	err = writer.Error()
	if err != nil {
		fmt.Println(err)
	}
	writer.Flush()
	return nil
}
開發者ID:BenDoan,項目名稱:DebtTracker,代碼行數:29,代碼來源:debttracker.go

示例14: main

func main() {
	if len(util.FlagCpuProf) > 0 {
		f := util.CreateFile(util.FlagCpuProf)
		pprof.StartCPUProfile(f)
		defer f.Close()
		defer pprof.StopCPUProfile()
	}
	if len(flagGobIt) > 0 {
		astralDir := util.Arg(0)
		dists := readAlignmentDists(astralDir)
		enc := gob.NewEncoder(util.CreateFile(flagGobIt))
		util.Assert(enc.Encode(dists), "Could not GOB encode distances")
		return
	}

	var dists *intern.Table
	if util.IsDir(util.Arg(0)) {
		dists = readAlignmentDists(util.Arg(0))
	} else {
		dec := gob.NewDecoder(util.OpenFile(util.Arg(0)))
		util.Assert(dec.Decode(&dists), "Could not GOB decode distances")
	}

	treeFile := util.Arg(1)
	outPath := util.Arg(2)

	treeReader := newick.NewReader(util.OpenFile(treeFile))
	tree, err := treeReader.ReadTree()
	util.Assert(err, "Could not read newick tree")

	csvw := csv.NewWriter(util.CreateFile(outPath))
	clusters := treeClusters(flagThreshold, dists, tree)
	util.Assert(csvw.WriteAll(clusters))
}
開發者ID:ndaniels,項目名稱:tools,代碼行數:34,代碼來源:main.go

示例15: main

func main() {
	app := initApp()

	app.Action = func(c *cli.Context) {
		// Validate arguments
		opts, err := validateArgs(c)
		if err != nil {
			log.WithFields(log.Fields{
				"message": err,
			}).Fatal("invalid arguments")
		}

		// Parse input, send, and receieve results
		header, results, err := sendAndReceive(opts.Input)
		if err != nil {
			log.WithFields(log.Fields{
				"message": err,
			}).Fatal("error during send and receive")
		}

		// Check for empty results (not probable)
		if len(results) < 1 {
			log.Info("No results!")
			return
		}

		// Sort and output results
		sort.Sort(ByASNumber(results))

		// Write to CSV if neecessary
		if opts.Output != "" {
			f, err := os.Create(opts.Output)
			if err != nil {
				log.Fatal(err)
			}
			defer f.Close()

			w := csv.NewWriter(f)
			header := []string{"AS", "IP", "BGP Prefix", "CC", "Registry", "Allocated", "AS Name"}
			err = w.Write(header)
			if err != nil {
				log.Fatal(err)
			}
			for _, res := range results {
				w.Write(res.CSVRecord())
			}
			w.Flush()
			log.WithFields(log.Fields{
				"path": opts.Output,
			}).Info("results saved to csv")
		} else {
			fmt.Printf(header)
			for _, res := range results {
				fmt.Printf("%s\n", res.String())
			}
		}
	}

	app.Run(os.Args)
}
開發者ID:hink,項目名稱:ip2asn,代碼行數:60,代碼來源:main.go


注:本文中的encoding/csv.NewWriter函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。