英文:
Compare CSV and conclusion of differences
问题
我开始编写一个程序来比较两个CSV文件。在阅读文档后,我找到了一个解决方案,但我不知道如何打印第二个文件中的差异,因为该函数只返回true/false。
package main
import (
"encoding/csv"
"fmt"
"os"
"reflect"
)
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)
}
请注意,这只是一个比较两个CSV文件并打印差异的示例程序。你可能需要进一步处理差异并进行适当的输出。
英文:
I started writing a program to compare two CSV files. After reading the documentation, I found a solution for I can't figure out how to print the differences from the second file since the function returns true/false
package main
import (
"encoding/csv"
"fmt"
"os"
"reflect"
)
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)
}
答案1
得分: 1
csv的ReadAll()函数返回行的切片,其中每一行都是列的切片。
我们可以循环遍历行,在每一行中再循环遍历列,并比较每一列的值。
以下是打印所有有差异的行及其行号的代码。
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)
// 打印有差异的行
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("第 %d 行: %v, %v\n", i+1, records[i], records2[i])
}
}
}
英文:
The csv ReadAll() function returns slice of rows, where row is a slice of columns.<br>
We can loop over the rows, and within a row, again loop over the columns and compare each column value.<br><br>
Here is the code that prints all lines that have differences alongwith their line numbers.
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])
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论