本文整理汇总了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
示例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
示例16: LiveExportHandler
func LiveExportHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
fmt.Println("Exporting live translations")
translations := model.GetLiveTranslations()
w.Header().Set("Content-Encoding", "UTF-8")
w.Header().Set("Content-Type", "application/csv; charset=UTF-8")
w.Header().Set("Content-Disposition", "attachment; filename=\"live.csv\"")
out := csv.NewWriter(w)
out.Write([]string{
"Original",
"Part of",
"Language",
"Translation",
})
for _, translation := range translations {
for _, part := range translation.Parts {
out.Write([]string{
part.Entry.Original,
part.Entry.PartOf,
translation.Language,
part.Translation,
})
}
}
out.Flush()
return
}
}
开发者ID:1Lykaon,项目名称:charactersheets-translator,代码行数:30,代码来源:translate.go
示例17: startWriteMode
func (q *Query) startWriteMode() error {
var e error
ci := q.Connection().(*Connection).Info()
if q.newFileWrite {
q.tempfile, e = os.OpenFile(q.filePath+".temp", os.O_CREATE, 0666)
} else {
q.tempfile, e = os.OpenFile(q.filePath, os.O_RDWR|os.O_APPEND, 0666)
}
if e != nil {
return e
}
q.writer = csv.NewWriter(q.tempfile)
if delimiter := ci.Settings.Get("delimiter", "").(string); delimiter != "" {
q.writer.Comma = rune(delimiter[0])
}
if q.newFileWrite && q.isUseHeader && len(q.headerColumn) > 0 {
datatemp := make([]string, 0)
for _, v := range q.headerColumn {
datatemp = append(datatemp, v.name)
}
q.writer.Write(datatemp)
q.writer.Flush()
}
return nil
}
开发者ID:rinosukmandityo,项目名称:dbox,代码行数:31,代码来源:query.go
示例18: NewCSVExportOutput
// NewCSVExportOutput returns a CSVExportOutput configured to write output to the
// given io.Writer, extracting the specified fields only.
func NewCSVExportOutput(fields []string, out io.Writer) *CSVExportOutput {
return &CSVExportOutput{
fields,
0,
csv.NewWriter(out),
}
}
开发者ID:devsaurin,项目名称:mongo-tools,代码行数:9,代码来源:csv.go
示例19: CSV
func (res *Result) CSV() []byte {
buff := &bytes.Buffer{}
writer := csv.NewWriter(buff)
writer.Write(res.Columns)
for _, row := range res.Rows {
record := make([]string, len(res.Columns))
for i, item := range row {
if item != nil {
record[i] = fmt.Sprintf("%v", item)
} else {
record[i] = ""
}
}
err := writer.Write(record)
if err != nil {
fmt.Println(err)
break
}
}
writer.Flush()
return buff.Bytes()
}
开发者ID:mavencode01,项目名称:pgweb,代码行数:28,代码来源:result.go
示例20: Run
func Run() {
records := [][]string{
{"first name", "last name", "username"},
{"Rob", "Pike", "rob"},
{"Ken", "Thompson", "ken"},
{"Robert", "Griesemer", "gri"},
}
fmt.Println(records[1][0])
w := csv.NewWriter(os.Stdout)
w.Comma = ':'
for _, record := range records {
if err := w.Write(record); err != nil {
log.Fatalln("error while writing record to csv", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}
开发者ID:jeob32,项目名称:gofun,代码行数:25,代码来源:code.go
注:本文中的encoding/csv.NewWriter函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论