这是我的代码:
package main
import (
"fmt"
"os"
"encoding/csv"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Error: Source file name is required\n")
fmt.Println("Usage:", os.Args[0], "<filename> \n")
return
}
file, err := os.Open(os.Args[1])
if err != nil {
fmt.Println("Error:", err)
return
}
// deferred call to Close() at the end of current method
defer file.Close()
//get a new cvsReader for reading file
reader := csv.NewReader(file)
//Configure reader options Ref http://golang.org/src/pkg/encoding/csv/reader.go?s=#L81
reader.Comma = '|'
reader.Comment = '#' //Comment character
reader.FieldsPerRecord = -1 //Number of records per record. Set to Negative value for variable
reader.TrimLeadingSpace = true
record, err := reader.Read()
fmt.Println(record)
result, _ := reader.ReadAll()
fmt.Println(result)
for i := range result {
// Element count.
fmt.Printf("Elements: %v", len(result[i]))
fmt.Println()
// Elements.
fmt.Println(result[i])
}
}
csv是这样的:
<iframe src="http://www.testdata.com/embed/ph5721952ef0c4e" frameborder="0" height="481" width="608" scrolling="no"></iframe>|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)1.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)2.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)3.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)4.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)5.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)6.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)7.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)8.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)9.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)10.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)11.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)12.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)13.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)15.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)16.jpg| it's done|allhomedata;hardware;wf;couple;ITwjob;dsshot;small-tera;forceager;wf;tamed-cat;bg-wf-chicken;cowanimal;tracy-rod;tamed;ds-on-table|wf;dsshots;Hardware;force||976|51703|122|40
<iframe src="http://www.testdata.com/embed/ph5721952ef0c4e" frameborder="0" height="481" width="608" scrolling="no"></iframe>|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)1.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)2.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)3.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)4.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)5.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)6.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)7.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)8.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)9.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)10.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)11.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)12.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)13.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)15.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)16.jpg| it's done|allhomedata;hardware;wf;couple;ITwjob;dsshot;small-tera;forceager;wf;tamed-cat;bg-wf-chicken;cowanimal;tracy-rod;tamed;ds-on-table|wf;dsshots;Hardware;force||976|51703|122|40
<iframe src="http://www.testdata.com/embed/ph5721952ef0c4e" frameborder="0" height="481" width="608" scrolling="no"></iframe>|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)1.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)2.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)3.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)4.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)5.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)6.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)7.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)8.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)9.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)10.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)11.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)12.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)13.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)15.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)16.jpg| it's done|allhomedata;hardware;wf;couple;ITwjob;dsshot;small-tera;forceager;wf;tamed-cat;bg-wf-chicken;cowanimal;tracy-rod;tamed;ds-on-table|wf;dsshots;Hardware;force||976|51703|122|40
<iframe src="http://www.testdata.com/embed/ph5721952ef0c4e" frameborder="0" height="481" width="608" scrolling="no"></iframe>|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg|http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)1.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)2.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)3.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)4.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)5.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)6.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)7.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)8.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)9.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)10.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)11.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)12.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)13.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)14.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)15.jpg;http://i0.cdn2b.image.testdata.zhncdn.com/videos/201604/28/75158391/original/(m=eaf88daaaa)16.jpg| it's done|allhomedata;hardware;wf;couple;ITwjob;dsshot;small-tera;forceager;wf;tamed-cat;bg-wf-chicken;cowanimal;tracy-rod;tamed;ds-on-table|wf;dsshots;Hardware;force||976|51703|122|40
但输出如下: [] []
我不明白为什么[]出现.... 如果有人知道为什么文件读取似乎是空的。
似乎阅读过程似乎已被打破。
感谢和问候
答案 0 :(得分:1)
你的主要问题是你没有检查错误,至少在你不是的例子中。如果您检查reader.Read()返回的错误,您会知道它是ErrBareQuote,为了避免该错误,您必须将csv.Reader的LazyQuotes字段设置为true。这是您的代码https://play.golang.org/p/3y-T1DCsp6
的有效版本