@@ -447,6 +447,117 @@ func (c *CSV_Utils_Go) summerize(preview_rows int) {
447
447
}
448
448
449
449
450
+ // remove_duplicates removes duplicate rows based on the specified column.
451
+ func (c * CSV_Utils_Go ) remove_duplicates (column string , output_file_name string ) [][]string {
452
+ /*
453
+ remove all duplictae values from the given `column`
454
+ */
455
+
456
+ fmt .Println ("REMOVING DUPS !! DEBUG !" )
457
+
458
+ col_idx , err := c .get_column_index (column )
459
+
460
+ if err != nil {
461
+ fmt .Println (err )
462
+ panic (err )
463
+ }
464
+
465
+ seen := make (map [string ]bool )
466
+ unique_rows := make ([][]string , 0 )
467
+
468
+ for _ , row := range c .rows {
469
+ key := row [col_idx ]
470
+ if ! seen [key ] {
471
+ seen [key ] = true
472
+ unique_rows = append (unique_rows , row )
473
+ }
474
+ }
475
+
476
+ c .rows = unique_rows
477
+ combined := append ([][]string {c .headers }, c .rows ... )
478
+
479
+ c ._update_csv (output_file_name , combined , "remove_duplicates()" )
480
+
481
+ return combined
482
+ }
483
+
484
+
485
+ // replace_first_val replaces the first occurrence of old_val with new_val in the specified column.
486
+ func (c * CSV_Utils_Go ) replace_first_val (column string , old_val string , new_val string , output_file_name string ) error {
487
+ col_idx , err := c .get_column_index (column )
488
+ if err != nil {
489
+ return err
490
+ }
491
+
492
+ for i := range c .rows {
493
+ if strings .EqualFold (c.rows [i ][col_idx ], old_val ) {
494
+ c.rows [i ][col_idx ] = new_val
495
+ break
496
+ }
497
+ }
498
+
499
+ combined := append ([][]string {c .headers }, c .rows ... )
500
+ c ._update_csv (output_file_name , combined , "`replace_first_val()`" )
501
+ return nil
502
+ }
503
+
504
+
505
+ // replace_all_vals replaces all occurrences of old_val with new_val in the specified column.
506
+ func (c * CSV_Utils_Go ) replace_all_vals (column string , old_val string , new_val string , output_file_name string ) error {
507
+ /*
508
+ Replaces all occurrences of `old_val` with `new_val` in the specified column
509
+ and updates the file or creates new file if given output file name.
510
+
511
+ :param column: Column name where the replacement should occur.
512
+ :param old_val: The value to be replaced.
513
+ :param new_val: The new value to replace with.
514
+ :output_file_name: The new file name in which updated data must be written
515
+ */
516
+ col_idx , err := c .get_column_index (column )
517
+ if err != nil {
518
+ return err
519
+ }
520
+
521
+ for i := range c .rows {
522
+ if strings .EqualFold (c.rows [i ][col_idx ], old_val ) {
523
+ c.rows [i ][col_idx ] = new_val
524
+ }
525
+ }
526
+
527
+ combined := append ([][]string {c .headers }, c .rows ... )
528
+ c ._update_csv (output_file_name , combined , "`replace_all_vals()`" )
529
+ return nil
530
+ }
531
+
532
+
533
+ // is_palindrome is a static method to check if a given word is a palindrome.
534
+ func CSV_Utils_Py_is_palindrome (word string ) bool {
535
+ runes := []rune (word )
536
+ n := len (runes )
537
+ for i := 0 ; i < n / 2 ; i ++ {
538
+ if runes [i ] != runes [n - 1 - i ] {
539
+ return false
540
+ }
541
+ }
542
+ return true
543
+ }
544
+
545
+
546
+ // count_valid_palindromes counts palindrome words in the CSV rows.
547
+ func (c * CSV_Utils_Go ) count_valid_palindromes () int {
548
+ count := 0
549
+ for _ , row := range c .rows {
550
+ for _ , word := range row {
551
+ word = strings .ToUpper (strings .TrimSpace (word ))
552
+ if word != "" && CSV_Utils_Py_is_palindrome (word ) {
553
+ count ++
554
+ }
555
+ }
556
+ }
557
+ return count
558
+ }
559
+
560
+
450
561
func main () {
451
562
// This main function is provided for testing purposes.
452
563
// It can be modified as needed to test the functionality of CSV_Utils_Py.
@@ -457,7 +568,17 @@ func main() {
457
568
fmt .Println (err )
458
569
return
459
570
}
571
+
460
572
csvUtil .display_csv (3 , true )
461
- csvUtil .summerize (3 )
462
573
574
+ csvUtil .remove_duplicates ("Job Title" , "./output.csv" )
575
+
576
+ csvUtil .replace_all_vals ("First Name" , "Shelby" , "Radha" , "./output.csv" )
577
+ // output_file_name if empty then the original csv file is modified
578
+ csvUtil .replace_first_val ("First Name" , "Radha" , "Kanha" , "./output.csv" )
579
+
580
+ csvUtil .count_valid_palindromes ()
581
+
582
+ // show first and last rows of the file
583
+ csvUtil .summerize (3 )
463
584
}
0 commit comments