0

How can I delete a row which contains null values from a comma separated csv file in c# ?

Example:
|      FirstName      |     LastName     |      Email    |    Address   |
|---------------------|------------------|---------------|--------------|
|          lmn        |         lmn      |[email protected]    |DemoAddress   |
|          xy         |         xy       |[email protected]      |DemoAddress   |
|          demo       |         demo     |               |              |
|          demo2      |         demo2    |[email protected]      |DemoAddress   |

    

Outcome:
|      FirstName      |     LastName     |      Email    |    Address   |
|---------------------|------------------|---------------|--------------|
|          lmn        |         lmn      |[email protected]    |DemoAddress   |
|          xy         |         xy       |[email protected]      |DemoAddress   |
|          demo2      |         demo2    |[email protected]      |DemoAddress   |

I tried the following code but doesn't work as expected

        private string filterCSV(string strFilePath) 
        { 
            var columnIndex = 3;
            var line = File.ReadAllLines(strFilePath);
            var n = line.Where(x => x[columnIndex].Equals(""));
            string result = string.Join("\r", not.ToArray());

            return result;

        }
4
  • 3
    There are many different ways. The simplest one is to read the file, process it, write to a temporary file, swap and delete the old file. Commented Aug 19, 2022 at 7:32
  • There's really no way to just remove part of a file. It's basically a case of read it in, do what you need to, then write it out. The options are in the details. If it's a small file then the simplest option is to read all the lines into a collection, remove the ones you don't want, then write the collection back out to the file. If it's a large file, you're better reading the lines one by one and writing the ones you want to keep out to a new file, then moving the new file to overwrite the original file. Commented Aug 19, 2022 at 7:41
  • Use autofilter :-) Commented Aug 19, 2022 at 9:00
  • I have edited the question with the code I tried.. would really appreciate if anyone can help out Commented Aug 21, 2022 at 13:38

1 Answer 1

1

Adding answer for future reference

private void RemoveBlanks(string datapath)
        {
            List<CSV> records;
            using (var reader = new StreamReader(datapath))
            using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                records = csv.GetRecords<CSV>().ToList();
                for(int i = 0; i < records.Count;++i)
                {
                    if (records[i].Email== "" && records[i].Address == "")
                    {
                        
                        records.RemoveAt(i);
                    }
                }

            }
            using (var writer = new StreamWriter(datapath))
            using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                csv.WriteRecords(records);
            }           
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.