本文整理匯總了Golang中encoding/csv.Reader.TrimLeadingSpace方法的典型用法代碼示例。如果您正苦於以下問題:Golang Reader.TrimLeadingSpace方法的具體用法?Golang Reader.TrimLeadingSpace怎麽用?Golang Reader.TrimLeadingSpace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類encoding/csv.Reader
的用法示例。
在下文中一共展示了Reader.TrimLeadingSpace方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *ParseCSV) Run() {
var tree *jee.TokenTree
var path string
var err error
var headers []string
var csvReader *csv.Reader
for {
select {
case ruleI := <-b.inrule:
// set a parameter of the block
path, err = util.ParseString(ruleI, "Path")
if err != nil {
b.Error(err)
continue
}
token, err := jee.Lexer(path)
if err != nil {
b.Error(err)
continue
}
tree, err = jee.Parser(token)
if err != nil {
b.Error(err)
continue
}
headers, err = util.ParseArrayString(ruleI, "Headers")
if err != nil {
b.Error(err)
continue
}
case <-b.quit:
// quit the block
return
case msg := <-b.in:
// deal with inbound data
if tree == nil {
continue
}
var data string
dataI, err := jee.Eval(tree, msg)
if err != nil {
b.Error(err)
continue
}
switch value := dataI.(type) {
case []byte:
data = string(value[:])
case string:
data = value
default:
b.Error("data should be a string or a []byte")
continue
}
csvReader = csv.NewReader(strings.NewReader(data))
csvReader.TrimLeadingSpace = true
// allow records to have variable numbers of fields
csvReader.FieldsPerRecord = -1
case <-b.inpoll:
if csvReader == nil {
b.Error("this block needs data to be pollable")
break
}
record, err := csvReader.Read()
if err != nil && err != io.EOF {
b.Error(err)
continue
}
row := make(map[string]interface{})
for fieldIndex, field := range record {
if fieldIndex >= len(headers) {
row[strconv.Itoa(fieldIndex)] = field
} else {
header := headers[fieldIndex]
row[header] = field
}
}
b.out <- row
case MsgChan := <-b.queryrule:
// deal with a query request
MsgChan <- map[string]interface{}{
"Path": path,
"Headers": headers,
}
}
}
}
示例2: ImportDictionaries
func ImportDictionaries() map[string][]*models.SuggestItem {
var itemMap = make(map[string][]*models.SuggestItem)
fileInfo, err := ioutil.ReadDir(DataDirectory)
if err != nil {
log.Print(err)
return itemMap
}
numberOfDictionaries := 0
for _, file := range fileInfo {
if !file.IsDir() && (strings.HasSuffix(file.Name(), ".txt") || strings.HasSuffix(file.Name(), ".txt.gz")) {
dictionaryFile := fmt.Sprintf("%s%s%s", DataDirectory, string(os.PathSeparator), file.Name())
dictionaryName := strings.TrimSuffix(strings.TrimSuffix(file.Name(), ".gz"), ".txt")
log.Printf("Importing dictionary %s from file %s", dictionaryName, dictionaryFile)
csvFile, err := os.Open(dictionaryFile)
if err != nil {
log.Print(err)
continue
}
defer csvFile.Close()
var csvReader *csv.Reader
if strings.HasSuffix(file.Name(), ".txt.gz") {
gzipReader, gzerr := gzip.NewReader(csvFile)
if gzerr == nil {
defer gzipReader.Close()
csvReader = csv.NewReader(gzipReader)
} else {
log.Print(gzerr)
continue
}
} else {
csvReader = csv.NewReader(csvFile)
}
csvReader.FieldsPerRecord = 2
csvReader.Comma = '|'
csvReader.LazyQuotes = true
csvReader.TrimLeadingSpace = true
rawCSVdata, err := csvReader.ReadAll()
if err != nil {
log.Print(err)
continue
}
for _, each := range rawCSVdata {
var suggestItem = new(models.SuggestItem)
suggestItem.Term = each[0]
weight, err := strconv.Atoi(each[1])
if err == nil {
suggestItem.Weight = weight
itemMap[dictionaryName] = append(itemMap[dictionaryName], suggestItem)
}
}
numberOfDictionaries++
}
}
log.Printf("Imported %d dictionaries", numberOfDictionaries)
return itemMap
}