rulururu

post Basic but reusable: Reading CSV

April 16th, 2008

Filed under: .NET — Kai @ 9:54 am

First, let’s recite the rules of CSV: Each line in a text file represents a record. The fields on each line are separated by commas.

Generally a CSV file should be abide by this rules:

  • Each record is one line (with exceptions)
  • Fields are separated with commas
  • Leading and trailing space-characters adjacent to comma field separators are ignored
  • Fields with embedded commas must be delimited with double-quote characters
  • Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
  • A field that contains embedded line-breaks must be surounded by double-quotes
  • Fields with leading or trailing spaces must be delimited with double-quote characters
  • Fields may always be delimited with double quotes
  • The first record in a CSV file may be a header record containing column (field) names

Take for example the next trivial CSV file:

alice, bob, eve, trudy
alice said: "don't move","""I won't"", he replied"

The first line parses into four separate fields (”alice”, “bob”, “eve”, “trudy”). The second one is trickier, but it produces two values.

The easiest implementation would be this:

private void readFile(String filePath)
{
  TextReader tx;
            try {
                tx = new StreamReader(filePath);
                String line;
 
                while ((line = tx.ReadLine()) != null)
                {
                    parseData(line);
                }
 
                tx.Close();
            } 
            catch(IOException ex) 
            {
                Console.WriteLine(ex.Message);
            }
}

Can be simply used that way:

private void parseData(String d)
{
            String[] sepData = d.Split(',');
            foreach(String d in sepData)
            {
                Console.WriteLine(d);
            }
}

And as you can guess, the code produces output like this:

> alice
> bob
> eve
> trudy

Those are just basics but they are reusable in some projects.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)