亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復golang中CSV文件中缺少逗號的“錯誤數量的字段”?

如何修復golang中CSV文件中缺少逗號的“錯誤數量的字段”?

Go
一只斗牛犬 2022-06-01 11:01:07
對于我的 CSV 文件,每一行都有相同的列數,但對于最后一行,那只有一列,所以當我用“foreach”讀取文件數據以獲得總行數時,我遇到了錯誤字段數,如何在不刪除最后一行的情況下修復此錯誤?此處的代碼和 CSV 文件:openfile, err := os.Open(filepath)    checkError("Error in reading the file\n", err)    fmt.Println("Already open filepath :", filepath)    //read the data of file    filedata, err := csv.NewReader(openfile).ReadAll()    checkError("Error in reading the file\n", err)    leg := len(filedata)    fmt.Println("total no of rows:", leg)    close := make([]string, leg)    date := make([]string, leg)    open := make([]string, leg)    high := make([]string, leg)    low := make([]string, leg)    adjustclose := make([]string, leg)    volume := make([]string, leg)    for e, value := range filedata {        date[e] = value[0]        open[e] = value[1]        high[e] = value[2]        low[e] = value[3]        close[e] = value[4]        adjustclose[e] = value[5]        volume[e] = value[6]    }2020-03-24,21,21,21,21,21,52020-04-06,20.8,20.8,20.8,20.8,20.8,192020-04-07,20.4,20.4,20.4,20.4,20.4,52020-04-09,20.4,20.4,20.4,20.4,20.4,10292
查看完整描述

1 回答

?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

通過將FieldsPerRecord設置為負值來禁用 CSV 閱讀器中的記錄長度測試。


csvr := csv.NewReader(openfile)

csvr.FieldsPerRecord = -1

filedata, err := csvr.ReadAll()

測試應用代碼中的記錄長度:


for e, value := range filedata {

    if len(value) < 7 {

        continue  // skip short records

    }

    ...

}


查看完整回答
反對 回復 2022-06-01
  • 1 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號