Csvhelper - read / get a single column of all rows?

20.6k views Asked by At

Hi I'm using csvHelper to read in a csv files with a variable number of columns. The first row always contains a header row. The number of columns is unknown at first, sometimes there are three columns and sometimes there are 30+. The number of rows can be large. I can read in the csv file, but how do I address each column of data. I need to do some basic stats on the data (e.g. min, max, stddev), then write them out in a non csv format. Here is my code so far...

try{
    using (var fileReader = File.OpenText(inFile))
    using (var csvResult  = new CsvHelper.CsvReader(fileReader))
    {
        // read the header line   
        csvResult.Read();

        // read the whole file
        dynamic recs = csvResult.GetRecords<dynamic>().ToList();

        /* now how do I get a whole column ???
         * recs.getColumn ???
         * recs.getColumn['hadername'] ???
         */

    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }

Thanks

2

There are 2 answers

1
Ehsan88 On BEST ANSWER

I don't think the library is capable of doing so directly. You have to read your column from individual fields and add them to a List, but the process is usually fast because readers do job fast. For example if your desired column is of type string, the code would be like so:

List<string> myStringColumn= new List<string>();
using (var fileReader = File.OpenText(inFile))
    using (var csvResult  = new CsvHelper.CsvReader(fileReader))
    {
        while (csvResult.Read())
       {
         string stringField=csvResult.GetField<string>("Header Name");
         myStringColumn.Add(stringField);    
       }
    }
0
Torakami On
using (System.IO.StreamReader file = new System.IO.StreamReader(Server.MapPath(filepath)))
                        {
                            //Csv reader reads the stream
                            CsvReader csvread = new CsvReader(file);
                            while (csvread.Read())
                            {
                                int count = csvread.FieldHeaders.Count();
                                if (count == 55)
                                {
                                    DataRow dr = myExcelTable.NewRow();
                                    if (csvread.GetField<string>("FirstName") != null)
                                    {
                                        dr["FirstName"] = csvread.GetField<string>("FirstName"); ;
                                    }
                                    else
                                    {
                                        dr["FirstName"] = "";
                                    }

                                    if (csvread.GetField<string>("LastName") != null)
                                    {
                                        dr["LastName"] = csvread.GetField<string>("LastName"); ;
                                    }
                                    else
                                    {
                                        dr["LastName"] = "";
                                    }
                                }
                                else
                                {
                                    lblMessage.Visible = true;
                                    lblMessage.Text = "Columns are not in specified format.";
                                    lblMessage.ForeColor = System.Drawing.Color.Red;
                                    return;
                                }
                            }
                            }