简体   繁体   中英

Delete rows in a csv file

I have two files: Example1.csv and Example2.csv, note they are not comma-separated, but are saved with the 'csv' extension.

Example 1 has 1 column which has emails address only Example 2 has many columns in which it has the column that is there in example 1 csv file.

Example1.csv file

emails

abc@gmail.com

jhg@yahoo.com

...

...

Example 2.csv

Column1 column2 Column3 column4 emails

1 45 456 123 abc@gmail.com

2 89 898 254 jhg@yahoo.com

3 85 365 789 ...

Now i need to delete the rows in example2.csv that matches with data in example 1 file, for example: Row 1 and 2 should be removed as they both the email matches.

 string[] lines = File.ReadAllLines(@"C:\example2.csv");

 var emails = File.ReadAllLines(@"C:\example1.csv");

 List<string> linesToWrite = new List<string>();


 foreach (string s in lines)
 {
     String[] split = s.Split(' ');
         if (s.Contains(emails))
             linesToWrite.Remove(s);

 }
 File.WriteAllLines("file3.csv", linesToWrite);

This should work:

var emails = new HashSet<string>(File.ReadAllLines(@"C:\example1.csv").Skip(1));

File.WriteAllLines("file3.csv", File.ReadAllLines("C:\example2.csv").Where(line => !emails.Contains(line.Split(',')[4]));

It reads all of file one, puts all emails into a format where lookup is easy, then goes through all lines in the second file and writes only those to disk that don't match any of the existing emails in their 5th column. You may want to expand on many parts, for example there is little to no error handling. It also compares emails case-sensitive, although emails are normally not.

Variable line is not string, but string array, same as lines, you are reading it in the same way as lines.

Also this line

if (s.Contains(line))

is not correct. You are trying to check if a string contains an array. If you need to check if a line contains an email from list, then this will be better:

if (split.Intersect(line).Any())

So, here is the final code.

var lines = File.ReadAllLines(@"C:\example2.csv");   
var line = File.ReadAllLines(@"C:\example1.csv");

var linesToWrite = new List<string>();    

foreach (var s in lines)
{
    var split = s.Split(',');
    if (split.Intersect(line).Any())
    {
        linesToWrite.Remove(s);
    }

}

File.WriteAllLines("file3.csv", linesToWrite);
static void Main(string[] args)
    {
        var Example1CsvPath = @"C:\Inetpub\Poligon\Poligon\Resources\Example1.csv";
        var Example2CsvPath = @"C:\Inetpub\Poligon\Poligon\Resources\Example2.csv";
        var Example3CsvPath = @"C:\Inetpub\Poligon\Poligon\Resources\Example3.csv";

        var EmailsToDelete = new List<string>();
        var Result = new List<string>();

        foreach(var Line in System.IO.File.ReadAllLines(Example1CsvPath))
        {
            if (!string.IsNullOrWhiteSpace(Line) && Line.IndexOf('@') > -1)
            {
                EmailsToDelete.Add(Line.Trim());
            }
        }


        foreach (var Line in System.IO.File.ReadAllLines(Example2CsvPath))
        {
            if (!string.IsNullOrWhiteSpace(Line))
            {
                var Values = Line.Split(' ');

                if (!EmailsToDelete.Contains(Values[4]))
                {
                    Result.Add(Line);                        
                }
            }
        }

        System.IO.File.WriteAllLines(Example3CsvPath, Result);

    }

I know this is 4 years-old... But I've got some ideas from this and I like to share my solution...

The idea behind this code is a simple CSV, with maximum of about 20 lines (reeeeally maximum), so I've decided to make something basic and not use a DB for this.

My solution is to rescan the CSV saving all variables (that is not the same that I like to delete) into a list and after scanning the CSV, it writes the list into the CSV (removing the one I've passed {textBox1} )

    List<string> _ = new();

    try {
        using (var reader = new StreamReader($"{Main.directory}\\bin\\ip.csv")) {

            while (!reader.EndOfStream) {

                var line = reader.ReadLine();
                var values = line.Split(',');

                if (values[0] == textBox1.Text || values[1] == textBox2.Text)
                    continue;

                _.Add($"{values[0]},{values[1]},{values[2]},");

            }

        }

        File.WriteAllLines($"{Main.directory}\\bin\\ip.csv", _);

    } catch (Exception f) {
        MessageBox.Show(f.Message);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM