gpt4 book ai didi

csv - 去CSV文件解析

转载 作者:数据小太阳 更新时间:2023-10-29 03:26:32 26 4
gpt4 key购买 nike

我正在尝试对 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 读入一段字符串 slice )。

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 行。有谁知道为什么会这样?
我认为 PrintPrintln() 的行为相同,只是 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("")
}

最佳答案

for 循环中使用 fmt.Print(col) 时,您需要在数据后添加空格,例如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"

关于csv - 去CSV文件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39903251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com