Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all values from CSV into a List using CsvHelper

Tags:

c#

csv

csvhelper

So I've been reading that I shouldn't write my own CSV reader/writer, so I've been trying to use the CsvHelper library installed via nuget. The CSV file is a grey scale image, with the number of rows being the image height and the number columns the width. I would like to read the values row-wise into a single List<string> or List<byte>.

The code I have so far is:

using CsvHelper;  public static List<string> ReadInCSV(string absolutePath) {     IEnumerable<string> allValues;      using (TextReader fileReader = File.OpenText(absolutePath))     {         var csv = new CsvReader(fileReader);         csv.Configuration.HasHeaderRecord = false;         allValues = csv.GetRecords<string>     }      return allValues.ToList<string>(); } 

But allValues.ToList<string>() is throwing a:

CsvConfigurationException was unhandled by user code

An exception of type 'CsvHelper.Configuration.CsvConfigurationException' occurred in CsvHelper.dll but was not handled in user code

Additional information: Types that inherit IEnumerable cannot be auto mapped. Did you accidentally call GetRecord or WriteRecord which acts on a single record instead of calling GetRecords or WriteRecords which acts on a list of records?

GetRecords is probably expecting my own custom class, but I'm just wanting the values as some primitive type or string. Also, I suspect the entire row is being converted to a single string, instead of each value being a separate string.

like image 732
Ayb4btu Avatar asked Oct 23 '15 04:10

Ayb4btu


People also ask

What is CsvHelper in C#?

CsvHelper is powerful but easy to use library that supports most important . NET Framework types. It is possible to write CSV-files with custom structure and it is also possible to register types and let library to convert them to CSV automatically.

How to read CSV file using CsvHelper in c#?

C# CSV read data into objects Globalization; using CsvHelper; using var streamReader = File. OpenText("users. csv"); using var csvReader = new CsvReader(streamReader, CultureInfo. CurrentCulture); var users = csvReader.


2 Answers

According to @Marc L's post you can try this:

public static List<string> ReadInCSV(string absolutePath) {     List<string> result = new List<string>();     string value;     using (TextReader fileReader = File.OpenText(absolutePath)) {         var csv = new CsvReader(fileReader);         csv.Configuration.HasHeaderRecord = false;         while (csv.Read()) {            for(int i=0; csv.TryGetField<string>(i, out value); i++) {                 result.Add(value);             }         }     }     return result; } 
like image 89
masinger Avatar answered Sep 22 '22 10:09

masinger


If all you need is the string values for each row in an array, you could use the parser directly.

var parser = new CsvParser( textReader ); while( true ) {     string[] row = parser.Read();     if( row == null )     {         break;     } } 

http://joshclose.github.io/CsvHelper/#reading-parsing

Update

Version 3 has support for reading and writing IEnumerable properties.

like image 21
Josh Close Avatar answered Sep 23 '22 10:09

Josh Close