Created
June 25, 2024 02:50
-
-
Save BolajiOlajide/8d9158b4effa9a0ece729822c059a5f2 to your computer and use it in GitHub Desktop.
Remove duplicate emails from a csv and output the result to a new one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"fmt" | |
"os" | |
"strings" | |
) | |
func main() { | |
f, err := os.OpenFile("interest_2.csv", os.O_RDONLY, 0644) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
r := csv.NewReader(f) | |
records, err := r.ReadAll() | |
if err != nil { | |
panic(err) | |
} | |
recordMap := make(map[string]struct{}) | |
var rto [][]string | |
for idx, er := range records { | |
if idx == 0 { | |
continue | |
} | |
username := er[0] | |
email := strings.ToLower(er[1]) | |
if _, ok := recordMap[email]; ok { | |
continue | |
} | |
rto = append(rto, []string{username, email}) | |
recordMap[email] = struct{}{} | |
} | |
f2, err := os.Create("output.csv") | |
if err != nil { | |
panic(err) | |
} | |
defer f2.Close() | |
err = csv.NewWriter(f2).WriteAll(rto) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("We've got %d unique records.\n", len(recordMap)) | |
// fmt.Println("Hello, world!", len(rec)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment