-1

I do have have 4 columns within my CVS file, the first two columns have 2 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;
    }
}

As you can see in this image I would like the the commas at the beginning to be removed and only display rows who contains data inside the column.

enter image description here

5
  • 2
    What is the desired output? This is exactly like your previous question: How to skip some empty rows in csv file and continue reading rows with data? c# console application Commented Aug 15, 2016 at 14:44
  • data, data , data, data , data, data, data data, data, data Store Number,Store Name, this is the desired output i would like Commented Aug 15, 2016 at 14:46
  • Do you want your output to be a collection of strings from all columns together in one list? Commented Aug 15, 2016 at 14:47
  • yes i would want that Commented Aug 15, 2016 at 14:50
  • 1
    There are some good answers below but I really like Streamreader for problems like this. Commented Aug 15, 2016 at 15:01

4 Answers 4

0

You could check whether the first character in the line is a ,, if so you'll know that the first column is empty:

foreach (var line in fileContents)
{
    if(line.Trim().IndexOf(",") == 0)
    {
        // first column empty, skip
        continue;
    }
    Console.WriteLine(line);
}
Sign up to request clarification or add additional context in comments.

1 Comment

i do not wish to skip the full column but just the first two rows in each column1 and 2 as it is shown in the picture
0

In general you have to read csv file byte by byte to validate it and to read it correctly in case when some cells contain \n and ; inside of "

But for simple case you can use something like this:

foreach (var line in fileContents)
    {
        if (line.Split(',').Any(s => s == ""))
            continue;
        Console.WriteLine(line);
    }

And you also have one mistake in file reading function. It always returns empty list.

Comments

0

If you wish to get all the lines without empty data, then try this:

namespace ConsoleApplication1
{
    using System.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            string[] lines = new string[3];

            string data1 = "data1,data2,data3,data4";
            string data2 = ",,data3,data4";
            string data3 = "data1,data2,data3,";

            lines[0] = data1;
            lines[1] = data2;
            lines[2] = data3;

            var result = lines
                .Select(line => line.Split(','))
                .Where(linePart => linePart.All(part => !string.IsNullOrWhiteSpace(part)))
                .ToList();
        }
    }
}

result will contains a list of string[] lines which did not contain any empty data parts.

Comments

0

As explained in the comments and from previous question: you can use the SelectMany to get all your columns into separate items in your collection and then to filter out the empty ones:

public static IList<string> ReadFile(string fileName)
{
    File.ReadAllLines(fileName)
        .SelectMany(line => line.Split(','))
        .Where(item => !string.IsNullOrWhiteSpace(item))
        .ToList();
}

1 Comment

@user5813072 - Did this help you solve your problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.