我正在尝试对CSV文件进行非常简单的解析,但出于某种原因,它只读取文件的最后一行。
我尝试过使用reader.ReadAll()
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
csvfile, err := os.Open("somecsvfile.csv")
if err != nil {
fmt.Println(err)
return
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.FieldsPerRecord = -1 // see the Reader struct information below
rawCSVdata, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// sanity check, display to standard output
for _, each := range rawCSVdata {
fmt.Printf("email : %s and timestamp : %s\n", each[0], each[1])
}
}
我在这段代码中尝试了一个简单的reader.Read()
for {
row, err := csvr.Read()
if err != nil {
if err == io.EOF {
err = nil
}
}
fmt.Println(row)
}
我在网上找到的这些片段看起来都非常简单。 CSV包似乎也准确描述了我想要的内容(ReadAll()
应该将整个CSV读入一段字符串切片)。
CSV文件似乎也很好。在网上下载我有下面的内容(每行除了第一行外是一个单独的条目) 我做错了什么?
street,city,zip,state,beds,baths,sq__ft,type,sale_date,price,latitude,longitude
3526 HIGH ST,SACRAMENTO,95838,CA,2,1,836,Residential,Wed May 21 00:00:00 EDT 2008,59222,38.631913,-121.434879
51 OMAHA CT,SACRAMENTO,95823,CA,3,1,1167,Residential,Wed May 21 00:00:00 EDT 2008,68212,38.478902,-121.431028
编辑:
我想我发现了什么问题。在下面的代码中,如果我将fmt.Print(col)
更改为fmt.Println(col)
,我最终会打印每一行的每一列。如果我将其保留为fmt.Print(col)
,我只打印1行。有谁知道为什么会这样?
我认为Print
和Println()
的行为方式相同,只是println()
添加了一个新行?
// sanity check, display to standard output
for _, row := range rawCSVdata {
for _, col := range row {
_,err := fmt.Print(col)
if err != nil {
fmt.Println(err)
}
}
fmt.Println("")
}
答案 0 :(得分:0)
在fmt.Print(col)
圈内使用for
时,您需要在数据后添加空格,例如fmt.Print(i, " ")
(试试The Go Playground):
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
fmt.Print(i)
}
}
输出:
0123456789
这很好用,请在The Go Playground上尝试:
package main
import (
"encoding/csv"
"fmt"
"strings"
)
func main() {
reader := csv.NewReader(strings.NewReader(str))
reader.TrimLeadingSpace = true
reader.FieldsPerRecord = -1 // see the Reader struct information below
rawCSVdata, err := reader.ReadAll()
if err != nil {
panic(err)
}
for _, each := range rawCSVdata {
//fmt.Println(each)
fmt.Printf("%-15q %-150q \n", each[0], each[1])
}
fmt.Println()
for _, row := range rawCSVdata {
for _, col := range row {
//fmt.Print(col)
fmt.Printf("%30q ", col)
}
fmt.Println("")
}
}
var str = `street,city,zip,state,beds,baths,sq__ft,type,sale_date,price,latitude,longitude
3526 HIGH ST,SACRAMENTO,95838,CA,2,1,836,Residential,Wed May 21 00:00:00 EDT 2008,59222,38.631913,-121.434879
51 OMAHA CT,SACRAMENTO,95823,CA,3,1,1167,Residential,Wed May 21 00:00:00 EDT 2008,68212,38.478902,-121.431028`
输出:
"street" "city"
"3526 HIGH ST" "SACRAMENTO"
"51 OMAHA CT" "SACRAMENTO"
"street" "city" "zip" "state" "beds" "baths" "sq__ft" "type" "sale_date" "price" "latitude" "longitude"
"3526 HIGH ST" "SACRAMENTO" "95838" "CA" "2" "1" "836" "Residential" "Wed May 21 00:00:00 EDT 2008" "59222" "38.631913" "-121.434879"
"51 OMAHA CT" "SACRAMENTO" "95823" "CA" "3" "1" "1167" "Residential" "Wed May 21 00:00:00 EDT 2008" "68212" "38.478902" "-121.431028"