Click here to Skip to main content
12,072,403 members (58,013 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.2K views
10 bookmarked
Posted

A Simple CSV Reader

, 31 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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.

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)

Share

About the Author

George Swan
Student
Wales Wales
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralI did the same in C++ once. Pin
SoMad30-Mar-13 21:39
professionalSoMad30-Mar-13 21:39 
GeneralRe: I did the same in C++ once. Pin
George Swan31-Mar-13 10:46
memberGeorge Swan31-Mar-13 10:46 
GeneralRe: I did the same in C++ once. Pin
SoMad31-Mar-13 10:55
professionalSoMad31-Mar-13 10:55 
GeneralRe: I did the same in C++ once. Pin
George Swan31-Mar-13 11:14
memberGeorge Swan31-Mar-13 11:14 
GeneralRe: I did the same in C++ once. Pin
SoMad31-Mar-13 20:25
professionalSoMad31-Mar-13 20:25 
GeneralRe: I did the same in C++ once. Pin
George Swan31-Mar-13 20:57
memberGeorge Swan31-Mar-13 20:57 
GeneralRe: I did the same in C++ once. Pin
SoMad31-Mar-13 23:01
professionalSoMad31-Mar-13 23: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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 1 Apr 2013
Article Copyright 2013 by George Swan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid