1 回答

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
csv 讀入() 函數(shù)返回行的切片,其中行是列的切片。
我們可以遍歷行,并在一行中再次循環(huán)遍歷列并比較每個(gè)列值。
下面是打印所有行及其行號(hào)上存在差異的代碼。
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
file, err := os.Open("sms_in_max.csv")
if err != nil {
fmt.Println(err)
}
reader := csv.NewReader(file)
records, _ := reader.ReadAll()
fmt.Println(records)
file2, err := os.Open("sms_out.csv")
if err != nil {
fmt.Println(err)
}
reader2 := csv.NewReader(file2)
records2, _ := reader2.ReadAll()
fmt.Println(records2)
// allrs := reflect.DeepEqual(records, records2)
// fmt.Println(allrs)
// Prints lines at which there is difference
for i := range records {
diff := false
for j := range records[i] {
if records[i][j] != records2[i][j] {
diff = true
break
}
}
if diff {
fmt.Printf("Line %d: %v, %v\n", i+1, records[i], records2[i])
}
}
}
- 1 回答
- 0 關(guān)注
- 94 瀏覽
添加回答
舉報(bào)