比较CSV文件并得出差异的结论。

huangapple go评论65阅读模式
英文:

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 (
	&quot;encoding/csv&quot;
	&quot;fmt&quot;
	&quot;os&quot;
)

func main() {
	file, err := os.Open(&quot;sms_in_max.csv&quot;)
	if err != nil {
		fmt.Println(err)
	}
	reader := csv.NewReader(file)
	records, _ := reader.ReadAll()
	fmt.Println(records)
	file2, err := os.Open(&quot;sms_out.csv&quot;)
	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(&quot;Line %d: %v, %v\n&quot;, i+1, records[i], records2[i])
		}
	}
}

huangapple
  • 本文由 发表于 2021年8月17日 15:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/68813652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定