1

I do have have 5 column within my CVS file, the first two columns have 3 empty rows. I would like to skip these empty rows. I know that I have to loop through the file however I do not know how to do this process.

Any suggestions would be appreciate it.

public class Program
{
    static void Main(string[] args)
    {
        var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Santander .csv");
        var fileContents = ReadFile(filePath);
        foreach (var line in fileContents)
        {
            Console.WriteLine(line);
        }

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    public static IList<string> ReadFile(string fileName)
    {
        var results = new List<string>();
        var target = File.ReadAllLines(fileName).ToList();
        return results;
    }
}
7
  • 1
    I'm a bit confused with the empty columns/rows - can you add an example of how is your input? Commented Aug 15, 2016 at 11:46
  • column1, column2 column3, column4, column5. only column 3 to 5 contains data column 1 and 2 are empty Commented Aug 15, 2016 at 11:57
  • Can you please add an example of your data? You asked to remove blank lines but from what you say here they are not blank :) Commented Aug 15, 2016 at 12:01
  • Column1 Column 2 Column3 Column4 data data data data data data data data data data data data data data data data Commented Aug 15, 2016 at 13:00
  • Please edit your question and add it there - maybe even just a print screen of the csv - and better explain what you need Commented Aug 15, 2016 at 13:01

2 Answers 2

2

Use a Where clause to keep only rows that are not NullOrWhiteSpace (null, empty or only white spaces):

public static IList<string> ReadFile(string fileName)
{
    return File.ReadAllLines(fileName)
               .Where(line => !string.IsNullOrWhiteSpace(line))
               .ToList();
}

After better understanding what you are after for then: For each line use Split to get the different columns and then check that the first 2 are not empty:

public static IList<string> ReadFile(string fileName)
{
    return (from line in File.ReadAllLines(fileName)
            where !string.IsNullOrWhiteSpace(line)
            let columns = line.Split(',')
            where columns.Length >= 2 && 
                !string.IsNullOrWhiteSpace(columns[0]) &&
                !string.IsNullOrWhiteSpace(columns[1])
            select line).ToList();
}

Changed to syntax query just because in my opinion it is cleaer when we start needing things like let


If what you want is get all the column values from the file without the empty ones then:

public static IList<string> ReadFile(string fileName)
{
    File.ReadAllLines(fileName)
        .SelectMany(line => line.Split(','))
        .Where(item => !string.IsNullOrWhiteSpace(item))
        .ToList();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I've just realised there might be something missing in this. results is never updated, or am I missing something. I think it's true in the original question as well.
@RosieC - you are right. Copied the original and missed it :) Fixed
@user5813072 - As far as this question goes this answer solved your question. Please Consider marking it as solved and upvoting ? :)
0

If you are not familiar/comfortable with Linq then another aproach is like this.

public static IList<string> ReadFile(string fileName)
{
  var results = new List<string>();
  string[] target = File.ReadAllLines(fileName);
  foreach (string line in target)
  {
    var array = line.Split(','); //If your csv is seperated by ; then replace the , with a ;
    if (!string.IsNullOrWhiteSpace(array[0]) && !string.IsNullOrWhiteSpace(array[1]) && array.Length >= 2)
    results.Add(line);
  }
return results;
}

target can still be defined as var, but I've defined it as string[] to make it more obvious that you can then do foreach over the array.

However I like Gilad Green's solution using Linq. I'm less familiar with it so it's not the first solution I think of, but I think it's worth getting familiar with.

4 Comments

ive tried the code however its still not affecting the first 2 empty rows
This assumes the row is completely empty. As Gilad Green has been trying to get clarification of on your original question it seemed at first you were trying to get rid of blank lines but not so sure now. This method could be adapted so that inside the foreach loop you checked the contents of the columns and changed the if condition to check for the first two columns blank.
id just like to remove the empty rows in the 2 columns within my csv file to be more clear
I'm not sure it's more clear, because an "empty row" implies it's completely empty, not just the first two columns. But having read the chat with Gilad Green I think I know what you are looking for. I've edited my answer.

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.