Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Tip/Trick

A Simple CSV Reader

Rate me:
Please Sign up or sign in to vote.
4.94/5 (3 votes)
31 Mar 2013CPOL 27.3K   10   7
An easy way to read CSV files.

Introduction 

This tip details a simple method to read CSV files.

Using the code

The main problem  when reading CSV is to determine when a comma is being used to separate values from each other and when  a comma is not a separator but part of a value. The following snippet resolves this problem:

C#
if (c == ‘,’ && quoteCount % 2 == 0)
{
//comma is being used as a separator
}

Where variable c is the character under consideration and quoteCount is the running total of ‘”’ characters  in the string at this point. Using this method it is possible to write a simple, if not robust, CSV Reader in a few lines of code.

C#
public static List<List<string>> ReadCsv(string[] data, char separator)
{
    var rows = new List<List<string>>();
    foreach (string text in data)
    {
        int quoteCount = 0;
        int startIndex = 0;
        int endIndex = 0;
        var cols = new List<string>();
        foreach (char c in text)
        {
            if (c == '"')
            {
              quoteCount += 1;
            }
            else
            {
             if (c == separator && quoteCount % 2 == 0)
              {
               cols.Add(trimQuotes(text.Substring(startIndex, endIndex - startIndex)));
               startIndex = endIndex + 1;
              }
            }
            endIndex++;
        }
        cols.Add(trimQuotes(text.Substring(startIndex, endIndex - startIndex)));
        rows.Add(cols);
    }
    return rows;
}

private static string trimQuotes(string text)
{
    if (String.IsNullOrEmpty(text))
    {
        return text;
    }
    return text[0] == '"' && text.Length > 2
               ? text.Substring(1, text.Length - 2).Replace("\"\"", "\"")
               : text.Replace("\"\"", "\"");
}

Conclusion

I am grateful to Jonathan Wood's excellent article, Reading and Writing CSV Files in C# , for the inspiration to write this tip. His method for parsing text into the CSV format is simply brilliant.  


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Wales Wales
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI did the same in C++ once. Pin
SoMad30-Mar-13 20:39
professionalSoMad30-Mar-13 20:39 
GeneralRe: I did the same in C++ once. Pin
George Swan31-Mar-13 9:46
mveGeorge Swan31-Mar-13 9:46 
GeneralRe: I did the same in C++ once. Pin
SoMad31-Mar-13 9:55
professionalSoMad31-Mar-13 9:55 
GeneralRe: I did the same in C++ once. Pin
George Swan31-Mar-13 10:14
mveGeorge Swan31-Mar-13 10:14 
GeneralRe: I did the same in C++ once. Pin
SoMad31-Mar-13 19:25
professionalSoMad31-Mar-13 19:25 
GeneralRe: I did the same in C++ once. Pin
George Swan31-Mar-13 19:57
mveGeorge Swan31-Mar-13 19:57 
GeneralRe: I did the same in C++ once. Pin
SoMad31-Mar-13 22:01
professionalSoMad31-Mar-13 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.