0

I have a .csv file which goes like this

"a1,b1,c1,d1", foo1, goo1 
"a2,b2,c2,d2", foo2, goo2

and I would like to parse it to an object of

string[] letters
string foo
string goo

so the data is

letters = [a1,b1,c1,d1]
foo = "foo1"
goo = "goo1"
--------------------
letters = [a2,b2,c2,d2]
foo = "foo2"
goo = "goo2"

now

line.Split(',');

won't work, how can I fix that?

c#

Yogi_Bear
  • 512
  • 2
  • 10
  • 24
  • 2
    https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file This may help you :) – KM.Thomas Dec 09 '19 at 11:08
  • 1
    Does this answer your question? [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – Ruben Martirosyan Dec 09 '19 at 11:26

2 Answers2

2

You say:

now line.Split(','); won't work

At first this does not make any sense, because you have not shown us any code using line.Split(), nor is there, under normal circumstances, any need to use line.Split() when dealing with CSV files.

So, what this is telling us is that instead of using a CSV library to read your CSV file, you are trying to parse the file yourself, with custom, hand-written code. And of course you are doing this in the most naive way possible, that is, with line.Split().

Don't do that.

Do yourself a favor and get the 'CSVHelper' library from NuGet and let it do your CSV parsing for you. That's the "normal circumstances" that I was talking about, and that's why normally line.Split() is not needed.

Then, once you have read your array field into a string, you can go ahead and use field.Split() on it.

The CSVHelper library is a bit of an overkill, as it does a lot more than what you actually need. For example, it can read lines from a CSV file directly into the members of a class representing a row of your CSV file. But it is okay: for several decades now, the entire software industry has been in the habit of using existing libraries for any trivial task at hand, even if only a tiny subset is needed from the entire wealth of functionality offered by the library.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

If you can get "a1,b1,c1,d1", foo1,goo1 into the same list, you can try this:

using System;
using System.Collections.Generic;

class MainClass 
{
  public static void Main (string[] args) 
  {
    List<String> items = new List<String>();
    string[] stuff = {"a1","b1","c1","d1", "foo1", "goo1"};
    items.AddRange(stuff);
    items.ForEach(i => Console.Write("{0}\t", i));
    List<String> letters = new List<String>();
    List<String> foo = new List<String>();
    List<String> goo = new List<String>();
    foreach(string a in items)
    {
      if(a.Contains("foo"))
      {
        foo.Add(a);
      }
      else if(a.Contains("goo"))
      {
        goo.Add(a);
      }
      else
      {
        letters.Add(a);
      }
    }
    letters.ForEach(i => Console.Write("{0}\t", i));
    foo.ForEach(i => Console.Write("{0}\t", i));
    goo.ForEach(i => Console.Write("{0}\t", i));
  }
}
Daniel Lawton
  • 416
  • 3
  • 9
  • 30