Hi all,

I am able to pull data from a CSV file into a string array using the following code.

        public static string[,] ReadCSV(string filename)
        {
            //Read Text
            string whole_file = System.IO.File.ReadAllText(filename);

            //Split into Lines
            whole_file = whole_file.Replace('\n', '\r');
            string[] lines = whole_file.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries);

            //Calucluate Rows and Columns
            int num_rows = lines.Length;
            int num_cols = lines[0].Split(',').Length;

            //Allocate Array
            string[,] values = new string[num_rows, num_cols];

            //Load Array
            for (int r = 0; r < num_rows; r++)
            {
                string[] line_r = lines[r].Split(',');

                for (int c = 0; c < num_cols; c++)
                {
                    values[r, c] = line_r[c];
                }
            }

            //Return Values
            return values;

        }

This was based off an online example, so I'm not entirely sure what's happening here. I'm assuming that the data is stored as a string array with two required indexes [row, column]... Though I've never seen that, so it causing a bit of confusion.

Anyway, all the data in the CSV is a double, and I need the array to contain only doubles. How would I go about either converting the entire array, or populating it as a double in the first place?

Recommended Answers

All 8 Replies

That's really helpful thanks. I was hoping for something I can apply to the whole array, but I guess I have to convert each entry under a loop.

I've taken another look at this and now have the following code:

static void ReadCSV(string filename)
        {
            using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename))
            {
                string[] fields = null;

                parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                parser.SetDelimiters(",");

                while (!parser.EndOfData)
                {
                    //Processing Row
                    fields = parser.ReadFields();
                    foreach (string field in fields)
                    { 
                       fields = AddItemToArray(fields, field);
                    }
                }
                Console.WriteLine(fields[17]);
            }
        }

The AddItemToArray code is:

        public static string[] AddItemToArray (string[] original, string itemToAdd)
        {
            string[] finalArray = new string[original.Length + 1];

            for (int i = 0; i < original.Length; i++)
            {
                finalArray[i] = original[i];
            }

            finalArray[finalArray.Length - 1] = itemToAdd;
            return finalArray;
        }

This should ensure that the final fields variable contains an array for every item in the CSV file. However, as you can see in the code I first posted, when I write Console.WriteLine(fields[17]); I get an index out of bounds error... The index only goes up to 4, which is the width of my columns... Why doesn't this store the complete data?

Btw my first two links in the previous post where switched. :)
Problem is in your AddItemToArray method. Her you define a local finalArray, which vanishes gracefully into oblivion, every time the method ends...
Better is to use a List:
Here is my code sketch on how to do it. Could use a little more erro checking, but I think you will find out how to proceed:

class Program
    {
        static List<double> Reals = new List<double>(); //-->use a List

        static void Main(string[] args)
        {
            ReadCSV("myfilePath");
        }

        static void ReadCSV(string filename)
        {
            using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename))
            {
                string[] fields = null;

                parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                parser.SetDelimiters(",");

                while (!parser.EndOfData)
                {
                    //Processing Row
                    fields = parser.ReadFields();
                    foreach (string field in fields)
                    {
                        //fields = AddItemToArray(fields, field);--> remove
                        Reals.Add(Convert.ToDouble(field));
                    }
                }
                Console.WriteLine(fields[17]);
            }
        }

Hmmm. This actually thows up the same problem. Any index greater than the number of columns through an error. It only seems to work for one row.

When I simply write Console.WriteLine(fields); I just get System.Double[] show up.

Did you try Console.WriteLine(Reals[17]); instead of fields ?

Haha. Whoops. Yea, that works - thanks for your help with this!!

Did you try Console.WriteLine(Reals[17]); instead of fields ?
EDIT: IT HAPPENED AGAIN!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.