本文整理汇总了Golang中encoding/csv.Read函数的典型用法代码示例。如果您正苦于以下问题:Golang Read函数的具体用法?Golang Read怎么用?Golang Read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Read函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compareToFixtures
func compareToFixtures(region holidays.Region, file string) error {
fd, err := os.Open(file)
if err != nil {
return fmt.Errorf("Couldn't open fixtures '%s': %s", file, err)
}
csv := csv.NewReader(fd)
for {
day, err := csv.Read()
if err == io.EOF {
break
}
dt, err := time.Parse("2006-01-02", day[0])
if err != nil {
return fmt.Errorf("Couldn't parse fixtures: %s", err)
}
t := dt.Add(5 * time.Hour)
holiday, err := holidays.Holiday(t, region)
if err != nil {
return fmt.Errorf("%s is supposed to be a holiday but library disagrees", t)
}
if holiday.Name != day[1] {
return fmt.Errorf("Holiday/library: %s, fixture: %s", holiday.Name, day[1])
}
}
return nil
}
示例2: NewRoute
// Prepares the route to be used in matching.
func NewRoute(method, path, action, fixedArgs, routesPath string, line int) (r *Route) {
// Handle fixed arguments
argsReader := strings.NewReader(fixedArgs)
csv := csv.NewReader(argsReader)
fargs, err := csv.Read()
if err != nil && err != io.EOF {
ERROR.Printf("Invalid fixed parameters (%v): for string '%v'", err.Error(), fixedArgs)
}
r = &Route{
Method: strings.ToUpper(method),
Path: path,
Action: action,
FixedParams: fargs,
TreePath: treePath(strings.ToUpper(method), path),
routesPath: routesPath,
line: line,
}
// URL pattern
if !strings.HasPrefix(r.Path, "/") {
ERROR.Print("Absolute URL required.")
return
}
actionSplit := strings.Split(action, ".")
if len(actionSplit) == 2 {
r.ControllerName = actionSplit[0]
r.MethodName = actionSplit[1]
}
return
}
示例3: produce
func produce(channel chan Prescription, filename string) {
//open file
fi, err := os.Open(filename)
if err != nil {
panic(err)
}
csv := csv.NewReader(fi)
skip := true
fmt.Println("producing")
for {
record, err := csv.Read()
if err != nil {
break
} else if err == io.EOF {
break
}
if skip != false {
skip = false
continue
}
pres := NewPrescription(record)
channel <- pres
}
close(channel)
}
示例4: csv2db
func csv2db(filepath string, max_rows int) DB {
// Read CSV file
csvFile, err := os.Open(filepath)
if err != nil {
panic(err)
}
defer csvFile.Close()
// For each line convert to struct
csv := csv.NewReader(csvFile)
columns, err := csv.Read()
if err != nil {
panic(err)
}
rows := []Row{}
for {
line, err := csv.Read()
if err == io.EOF || (max_rows == len(rows) && max_rows != -1) {
break
}
rows = append(rows, line)
}
return DB{columns, rows}
}
示例5: NewBinder
func NewBinder(reader io.Reader, opts *Options) (*Binder, error) {
csv := csv.NewReader(reader)
csv.FieldsPerRecord = -1
if opts == nil {
opts = &Options{}
} else {
if opts.Separator == 0 {
opts.Separator = ','
}
csv.Comma = opts.Separator
}
if opts.TimeZone == nil {
opts.TimeZone = time.UTC
}
var meta map[int]string
if len(opts.Header) == 0 {
header, err := csv.Read()
if err != nil {
return nil, err
}
meta = make(map[int]string)
for i, col := range header {
if opts.StripBOM && i == 0 {
// Remove BOM
col = strings.Replace(col, "\ufeff", "", -1)
}
meta[i] = col
}
if len(meta) == 0 {
return nil, ErrNoHeader
}
} else {
meta = opts.Header
if len(meta) == 0 {
return nil, ErrNoCustomHeader
}
}
return &Binder{csv: csv, meta: meta, opts: opts}, nil
}
示例6: readNanp
func readNanp(country string, data []byte) {
r := bytes.NewReader(data)
csv := csv.NewReader(r)
csv.FieldsPerRecord = -1
for {
row, err := csv.Read()
if err == io.EOF {
break
}
for i := 1; i < len(row); i++ {
area := areaCode{
name: row[0],
code: row[i],
countryName: country,
}
nanp[row[i]] = area
}
}
}
示例7: processFile
// processFile iterates over a csv file and adds the data
// to the main datastructure
func processFile(m Samples, rf io.Reader, a action) {
csv := csv.NewReader(rf)
header := true
var s_header []string
for {
s_line, err := csv.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal("Error reading from csv file in ulrness.processFile() ", " err: ", err)
}
if header {
s_header = s_line
header = false
} else {
err = a(m, s_line, s_header)
if err != nil {
log.Fatal("Error processing line in urlness.processFile()", "error: ", err)
}
}
}
}
示例8: NewRoute
// Prepares the route to be used in matching.
func NewRoute(method, path, action, fixedArgs string) (r *Route) {
// Handle fixed arguments
argsReader := strings.NewReader(fixedArgs)
csv := csv.NewReader(argsReader)
fargs, err := csv.Read()
if err != nil && err != io.EOF {
glog.Errorf("Invalid fixed parameters (%v): for string '%v'", err.Error(), fixedArgs)
}
r = &Route{
Method: strings.ToUpper(method),
Path: path,
Action: action,
FixedParams: fargs,
}
// URL pattern
// TODO: Support non-absolute paths
if !strings.HasPrefix(r.Path, "/") {
glog.Error("Absolute URL required.")
return
}
// Handle embedded arguments
// Convert path arguments with unspecified regexes to standard form.
// e.g. "/customer/{id}" => "/customer/{<[^/]+>id}
normPath := nakedPathParamRegex.ReplaceAllStringFunc(r.Path, func(m string) string {
var argMatches []string = nakedPathParamRegex.FindStringSubmatch(m)
return "{<[^/]+>" + argMatches[1] + "}"
})
// Go through the arguments
r.args = make([]*arg, 0, 3)
for i, m := range argsPattern.FindAllStringSubmatch(normPath, -1) {
r.args = append(r.args, &arg{
name: string(m[2]),
index: i,
constraint: regexp.MustCompile(string(m[1])),
})
}
// Now assemble the entire path regex, including the embedded parameters.
// e.g. /app/{<[^/]+>id} => /app/(?P<id>[^/]+)
pathPatternStr := argsPattern.ReplaceAllStringFunc(normPath, func(m string) string {
var argMatches []string = argsPattern.FindStringSubmatch(m)
return "(?P<" + argMatches[2] + ">" + argMatches[1] + ")"
})
r.pathPattern = regexp.MustCompile(pathPatternStr + "$")
// Handle action
var actionPatternStr string = strings.Replace(r.Action, ".", `\.`, -1)
for _, arg := range r.args {
var argName string = "{" + arg.name + "}"
if argIndex := strings.Index(actionPatternStr, argName); argIndex != -1 {
actionPatternStr = strings.Replace(actionPatternStr, argName,
"(?P<"+arg.name+">"+arg.constraint.String()+")", -1)
}
}
r.actionPattern = regexp.MustCompile(actionPatternStr)
return
}
示例9: parse
// parse runs Sadf on the previously saved tmpFile:
// Sadf -p -- -p <option> tmpFile
// and parses the output to add it to the telegraf.Accumulator acc.
func (s *Sysstat) parse(acc telegraf.Accumulator, option string, ts time.Time) error {
cmd := execCommand(s.Sadf, s.sadfOptions(option)...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("running command '%s' failed: %s", strings.Join(cmd.Args, " "), err)
}
r := bufio.NewReader(stdout)
csv := csv.NewReader(r)
csv.Comma = '\t'
csv.FieldsPerRecord = 6
var measurement string
// groupData to accumulate data when Group=true
type groupData struct {
tags map[string]string
fields map[string]interface{}
}
m := make(map[string]groupData)
for {
record, err := csv.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
device := record[3]
value, err := strconv.ParseFloat(record[5], 64)
if err != nil {
return err
}
tags := map[string]string{}
if device != "-" {
tags["device"] = device
if addTags, ok := s.DeviceTags[device]; ok {
for _, tag := range addTags {
for k, v := range tag {
tags[k] = v
}
}
}
}
if s.Group {
measurement = s.Options[option]
if _, ok := m[device]; !ok {
m[device] = groupData{
fields: make(map[string]interface{}),
tags: make(map[string]string),
}
}
g, _ := m[device]
if len(g.tags) == 0 {
for k, v := range tags {
g.tags[k] = v
}
}
g.fields[escape(record[4])] = value
} else {
measurement = s.Options[option] + "_" + escape(record[4])
fields := map[string]interface{}{
"value": value,
}
acc.AddFields(measurement, fields, tags, ts)
}
}
if s.Group {
for _, v := range m {
acc.AddFields(measurement, v.fields, v.tags, ts)
}
}
if err := internal.WaitTimeout(cmd, time.Second*5); err != nil {
return fmt.Errorf("command %s failed with %s",
strings.Join(cmd.Args, " "), err)
}
return nil
}