func writeSalesData(conn *sql.DB, writer *csv.Writer, first int, last int, wg *sync.WaitGroup) { defer wg.Done() rows, err := conn.Query("select * from sales where id between ? and ?", first, last) if err != nil { fmt.Println(err) os.Exit(1) } var orderId int var userId int var orderAmount float64 for rows.Next() { rows.Scan(&orderId, &userId, &orderAmount) writer.Write([]string{ strconv.Itoa(orderId), strconv.Itoa(userId), strconv.FormatFloat(orderAmount, 'f', 6, 64), }, ) } writer.Flush() }
func writeOneRecordAtaTime(writer *csv.Writer, data [][]string) { for _, row := range data { writer.Write(row) } writer.Flush() }
func writePromptInText(pId string, pc PromptConfig, writer *csv.Writer, level int, evalue ExpectedResponseValue) { indent := "" pPromptType := "" for i := 0; i < level; i++ { indent = indent + " " } if pId == "" { pId = pc.Id pPromptType = pc.PromptType } pId = indent + pId pText := pc.Text supportPromptId := pc.SupportPromptRef.Id var value = []string{strconv.Itoa(level), pId, pPromptType, evalue.Id, supportPromptId, "", "", ""} for i, v := range pText { value[5+i] = v } err := writer.Write(value) if err != nil { log.Fatal("Cannot write file", err) } level++ ecs := pc.ExpectedResponses.Values for _, v := range ecs { writePromptInText(v.NextPromptRef.Id, v.NextPrompt, writer, level, v) } }
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() }
func writeTo(writer *csv.Writer, in interface{}) error { inValue, inType := getConcreteReflectValueAndType(in) // Get the concrete type (not pointer) (Slice<?> or Array<?>) if err := ensureInType(inType); err != nil { return err } inInnerWasPointer, inInnerType := getConcreteContainerInnerType(inType) // Get the concrete inner type (not pointer) (Container<"?">) if err := ensureInInnerType(inInnerType); err != nil { return err } inInnerStructInfo := getStructInfo(inInnerType) // Get the inner struct info to get CSV annotations csvHeadersLabels := make([]string, len(inInnerStructInfo.Fields)) for i, fieldInfo := range inInnerStructInfo.Fields { // Used to write the header (first line) in CSV csvHeadersLabels[i] = fieldInfo.getFirstKey() } if err := writer.Write(csvHeadersLabels); err != nil { return err } inLen := inValue.Len() for i := 0; i < inLen; i++ { // Iterate over container rows for j, fieldInfo := range inInnerStructInfo.Fields { csvHeadersLabels[j] = "" inInnerFieldValue, err := getInnerField(inValue.Index(i), inInnerWasPointer, fieldInfo.IndexChain) // Get the correct field header <-> position if err != nil { return err } csvHeadersLabels[j] = inInnerFieldValue } if err := writer.Write(csvHeadersLabels); err != nil { return err } } writer.Flush() return writer.Error() }
func writeArrayOfScalars(writer *csv.Writer, vj reflect.Value) error { sa := make([]string, vj.Len()) for i := 0; i < vj.Len(); i++ { sa[i] = fmt.Sprintf("%v", reflect.Indirect(vj.Index(i))) } return writer.Write(sa) }
func writeStructFields(writer *csv.Writer, str reflect.Value, dataModel interface{}) error { sa := make([]string, str.NumField()) for i := 0; i < str.NumField(); i++ { sa[i] = fmt.Sprintf("%v", reflect.Indirect(str.Field(i))) } return writer.Write(sa) }
func csvExporter(c *csv.Writer, ms []*Metric) { lc := make(chan *LabelSet) quit := make(chan bool) for _, m := range ms { go m.EmitLabelSets(lc, quit) for { select { case l := <-lc: record := []string{m.Program, m.Name, m.Kind.String()} for k, v := range l.labels { record = append(record, k, v) } record = append(record, fmt.Sprintf("%s", l.datum.Time)) record = append(record, fmt.Sprintf("%d", l.datum.Get())) err := c.Write(record) if err != nil { log.Printf("Failed to write csv record %q: %s\n", record, err) } case <-quit: goto next } } next: } }
// NewWithCsvWriter writes all of the Rows in a Table to a CSV file using the options in the CSV writer. // It assumes that all Rows have the same headers. Columns are written in alphabetical order. func NewWithCsvWriter(writer *csv.Writer) optimus.Sink { return func(source optimus.Table) error { defer source.Stop() headers := []string{} wroteHeader := false for row := range source.Rows() { if !wroteHeader { headers = convertRowToHeader(row) sort.Strings(headers) 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 } }
func NewAppend(path string) *kvAppend { kv := &kvAppend{ kvMem: NewMemory(), queue: make(chan *record), } var lastErr error f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644) if err != nil { lastErr = err } else { r := csv.NewReader(f) for { rec, err := r.Read() if err != nil { if err != io.EOF { lastErr = err } break } kv.kvMem.m[rec[0]] = []byte(rec[1]) } } var w *csv.Writer if lastErr == nil { w = csv.NewWriter(f) } go func() { for r := range kv.queue { if lastErr == nil { var rec []string if r.Value != nil { rec = []string{r.Key, string(r.Value)} } else { rec = []string{r.Key} } if err = w.Write(rec); err != nil { lastErr = err } if r != nil && r.Err != nil { w.Flush() f.Sync() r.Err <- nil close(r.Err) } } else if r.Err != nil { r.Err <- lastErr } } if f != nil { f.Close() } }() return kv }
func writeArrayOfStringers(writer *csv.Writer, value reflect.Value) error { debug(" -- writeArrayOfStringers %d\n", value.Len()) sa := make([]string, value.Len()) for i := 0; i < value.Len(); i++ { sa[i] = fmt.Sprintf("%v", reflect.Indirect(value.Index(i)).Interface().(fmt.Stringer)) } return writer.Write(sa) }
func processResults(config *Config, resultChan <-chan *JobResult) map[string]*JobStats { var resultFile *csv.Writer var allTestStats = make(map[string]*JobStats) var recentTestStats = make(map[string]*JobStats) if len(*queryStatsFile) > 0 { if file, err := os.Create(*queryStatsFile); err != nil { log.Fatalf("Could not open result file %s: %v", *queryStatsFile, err) } else { defer file.Close() resultFile = csv.NewWriter(file) defer resultFile.Flush() } } ticker := time.NewTicker(*updateInterval) if !*intermediateUpdates { ticker.Stop() } defer ticker.Stop() for { select { case jr, ok := <-resultChan: if !ok { return allTestStats } if resultFile != nil { resultFile.Write([]string{ jr.Name, strconv.FormatInt(jr.Start.Nanoseconds()/1000, 10), strconv.FormatInt(jr.Elapsed.Nanoseconds()/1000, 10), strconv.FormatInt(jr.RowsAffected, 10), }) } if _, ok := allTestStats[jr.Name]; !ok { allTestStats[jr.Name] = new(JobStats) } if _, ok := recentTestStats[jr.Name]; !ok { recentTestStats[jr.Name] = new(JobStats) } allTestStats[jr.Name].Update(jr) recentTestStats[jr.Name].Update(jr) case <-ticker.C: for name, stats := range recentTestStats { log.Printf("%s: %v", name, stats) } recentTestStats = make(map[string]*JobStats) } } }
func writeHeader(w *csv.Writer, d interface{}) error { v := reflect.ValueOf(d) header := make([]string, v.NumField()) for i := 0; i < v.NumField(); i++ { header[i] = v.Type().Field(i).Name } if err := w.Write(header); err != nil { return err } return nil }
// Csv calls each day and then writes the month statistics in csv format func (fsm *FlightStatMonth) Csv(w *csv.Writer) { days := []int{} for d, _ := range fsm.Day { days = append(days, d) } sort.Ints(days) for _, d := range days { fsd := fsm.Day[d] fsd.Csv(w) } w.Write([]string{fmt.Sprintf("Total %s", fsm.Date.Format("January 2006")), fmt.Sprintf("%d", fsm.Flights), fmt.Sprintf("%.2f", fsm.Airtime.Minutes())}) }
// Takes a *result struct and writes out lines to *csv.Writer func writeCsvLine(w *csv.Writer, res *result) { headers_joined := strings.Join(res.headers, ",") // When we save to CSV duration is always in seconds duration_seconds := fmt.Sprintf("%v", res.time.Seconds()) // We need a array of strings for the csv package. record := []string{res.url, res.httpStatus, headers_joined, duration_seconds} err := w.Write(record) if err != nil { fmt.Println("Problems writing to csv file") } w.Flush() }
func (c Tweet) csvWriter(writer *csv.Writer, m chan Tweet) { var mutex = &sync.Mutex{} for i := range m { c = i //fmt.Println(c) data := []string{c.User, c.Post_date, c.Message} mutex.Lock() writer.Write(data) writer.Flush() mutex.Unlock() } }
// Csv calls each month and then writes the year statistics in csv format func (fsy *FlightStatYear) Csv(w *csv.Writer) { month := []int{} for m, _ := range fsy.Month { month = append(month, int(m)) } sort.Ints(month) for _, m := range month { fsm := fsy.Month[time.Month(m)] fsm.Csv(w) } w.Write([]string{fmt.Sprintf("Total %d", fsy.Year.Year()), fmt.Sprintf("%d", fsy.Flights), fmt.Sprintf("%.2f", fsy.Airtime.Minutes())}) }
func writeSif(nodes map[int64]string, edges []cx.Edge, w csv.Writer) { for i := range edges { edge := edges[i] if edge.I == "" { w.Write([]string{nodes[edge.S], "i", nodes[edge.T]}) } else { w.Write([]string{nodes[edge.S], edge.I, nodes[edge.T]}) } } w.Flush() }
func writeCsvRow(out *csv.Writer, name, matched, path string, rating float64, ratings int) { url := "" if path != "" { url = "https://play.google.com" + path } out.Write([]string{ name, matched, url, fmt.Sprintf("%f", rating), strconv.Itoa(ratings), }) }
func (c Tweet) CsvWriter(writer *csv.Writer, m chan Tweet) { var mutex = &sync.Mutex{} for i := range m { c = i //fmt.Println(c) data := []string{c.User, c.Post_date, c.Message} //Introduced locks for write to csv file mutex.Lock() writer.Write(data) writer.Flush() mutex.Unlock() //lock closed } }
func recurseprint(elem *ChronologyTreeStruct, csvwriter *csv.Writer, row *[]string, isocode string, level int, index int) { if index > 0 { csvwriter.Write(*row) *row = make([]string, 12) } if level > 0 { (*row)[(level-1)*3+0] = elem.Name[isocode] (*row)[(level-1)*3+1] = strconv.Itoa(dateToHuman(elem.Start_date)) (*row)[(level-1)*3+2] = strconv.Itoa(dateToHuman(elem.End_date)) } for i, e := range elem.Content { recurseprint(&e, csvwriter, row, isocode, level+1, i) } }
func TeamHeader(w *csv.Writer) { err := w.Write([]string{ "Scored", "Opponent Scored", "Possesions", "Efficiency", "D's", "Opponent Turns", "Percent D's", }) if err != nil { log.Fatal(err) } w.Flush() }
func (user *User) WriteCSV(writer *csv.Writer) { var fields []string = make([]string, 7) fields[0] = user.Name fields[1] = user.ContactInfo fields[2] = string(user.UserLevel) fields[3] = strings.Join(user.Sponsors, ";") if !user.ValidFrom.IsZero() { fields[4] = user.ValidFrom.Format("2006-01-02 15:04") } if !user.ValidTo.IsZero() { fields[5] = user.ValidTo.Format("2006-01-02 15:04") } fields[6] = strings.Join(user.Codes, ";") writer.Write(fields) }
func (t TeamStats) Summary(w *csv.Writer) { err := w.Write([]string{ fmt.Sprintf("%v", t.Scored), fmt.Sprintf("%v", t.OppScore), fmt.Sprintf("%v", t.Possesions), fmt.Sprintf("%.2f", 100*float64(t.Scored)/float64(t.Possesions)), fmt.Sprintf("%v", t.Ds), fmt.Sprintf("%v", t.OpponentTurns), fmt.Sprintf("%.2f", 100*float64(t.Ds)/float64(t.OpponentTurns)), }) if err != nil { log.Fatal(err) } w.Flush() }
func json2csv(r LineReader, w *csv.Writer, keys []string) { var line []byte var err error for { if err == io.EOF { return } line, err = r.ReadBytes('\n') if err != nil { if err != io.EOF { log.Printf("Input ERROR: %s", err) break } } if len(line) == 0 { continue } var data map[string]interface{} err = json.Unmarshal(line, &data) if err != nil { log.Printf("ERROR Json Decoding: %s - %v", err, line) continue } var record []string for _, key := range keys { if v, ok := data[key]; ok { switch v.(type) { case nil: record = append(record, "") case float64: f, _ := v.(float64) if math.Mod(f, 1.0) == 0.0 { record = append(record, fmt.Sprintf("%d", int(f))) } else { record = append(record, fmt.Sprintf("%f", f)) } default: record = append(record, fmt.Sprintf("%+v", v)) } } else { record = append(record, "") } } w.Write(record) w.Flush() } }
func json2csv(r LineReader, w *csv.Writer, keys []string, printHeader bool) { var line []byte var err error line_count := 0 var expanded_keys [][]string for _, key := range keys { expanded_keys = append(expanded_keys, strings.Split(key, ".")) } for { if err == io.EOF { return } line, err = r.ReadBytes('\n') if err != nil { if err != io.EOF { log.Printf("Input ERROR: %s", err) break } } line_count++ if len(line) == 0 { continue } if printHeader { w.Write(keys) w.Flush() printHeader = false } var data map[string]interface{} err = json.Unmarshal(line, &data) if err != nil { log.Printf("ERROR Decoding JSON at line %d: %s\n%s", line_count, err, line) continue } var record []string for _, expanded_key := range expanded_keys { record = append(record, get_value(data, expanded_key)) } w.Write(record) w.Flush() } }
func outputCSV(w *csv.Writer, p types.ComplainerProfile, c types.Complaint) { zip := regexp.MustCompile("^.*(\\d{5}(-\\d{4})?).*$").ReplaceAllString(p.Address, "$1") r := []string{ p.CallerCode, p.FullName, p.Address, zip, p.EmailAddress, fmt.Sprintf("%.4f", p.Lat), fmt.Sprintf("%.4f", p.Long), fmt.Sprintf("%d", c.Timestamp.UTC().Unix()), c.Timestamp.Format("2006/01/02"), c.Timestamp.Format("15:04:05"), c.Description, c.AircraftOverhead.FlightNumber, c.Activity, } if err := w.Write(r); err != nil { // ? } }
func saveDatasetAsCsv(dataset []Data, filePath string) { f, err := os.Create(filePath) if err != nil { log.Fatal(err) } defer f.Close() var w *csv.Writer if runtime.GOOS == "windows" { // on Windows, use Shift-JIS to open csv file via Microsoft Excel. converter := bufio.NewWriter(transform.NewWriter(f, japanese.ShiftJIS.NewEncoder())) w = csv.NewWriter(converter) } else { w = csv.NewWriter(f) } defer w.Flush() // Write header first header := []string{ // "日付", "証券会社名", "n225_sell", "n225_buy", "n225_net", "topix_sell", "topix_buy", "topix_net", "net_total", } w.Write(header) // Write dataset for _, data := range dataset { var record []string // record = append(record, obj.InfoDate) record = append(record, data.Company) record = append(record, data.N225Sell) record = append(record, data.N225Buy) record = append(record, data.N225Net) record = append(record, data.TopixSell) record = append(record, data.TopixBuy) record = append(record, data.TopixNet) record = append(record, data.NetTotal) w.Write(record) } }
func writeFromChan(writer *csv.Writer, c <-chan interface{}) error { // Get the first value. It wil determine the header structure. firstValue := <-c inValue, inType := getConcreteReflectValueAndType(firstValue) // Get the concrete type if err := ensureStructOrPtr(inType); err != nil { return err } inInnerWasPointer := inType.Kind() == reflect.Ptr inInnerStructInfo := getStructInfo(inType) // Get the inner struct info to get CSV annotations csvHeadersLabels := make([]string, len(inInnerStructInfo.Fields)) for i, fieldInfo := range inInnerStructInfo.Fields { // Used to write the header (first line) in CSV csvHeadersLabels[i] = fieldInfo.getFirstKey() } if err := writer.Write(csvHeadersLabels); err != nil { return err } write := func(val reflect.Value) error { for j, fieldInfo := range inInnerStructInfo.Fields { csvHeadersLabels[j] = "" inInnerFieldValue, err := getInnerField(val, inInnerWasPointer, fieldInfo.IndexChain) // Get the correct field header <-> position if err != nil { return err } csvHeadersLabels[j] = inInnerFieldValue } if err := writer.Write(csvHeadersLabels); err != nil { return err } return nil } if err := write(inValue); err != nil { return err } for v := range c { val, _ := getConcreteReflectValueAndType(v) // Get the concrete type (not pointer) (Slice<?> or Array<?>) if err := ensureStructOrPtr(inType); err != nil { return err } if err := write(val); err != nil { return err } } writer.Flush() return writer.Error() }
func process(node *html.Node, w *csv.Writer) error { for child := node.FirstChild; child != nil; child = child.NextSibling { if child.Type == html.ElementNode && child.Data == "a" { for _, a := range child.Attr { if a.Key == "href" { data := repoRegexp.FindStringSubmatch(a.Val) if len(data) == 4 { err := w.Write(data) return err } break } } } process(child, w) } return nil }