Read all values ​​from CSV to list using CsvHelper

So, I read that I should not write my own CSV reader / writer, so I tried to use the CsvHelper library installed via nuget. A CSV file is a grayscale image, with the number of lines being the height of the image and the number of columns wide. I would like to read the values ​​differently in one 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>() throws:

CsvConfigurationException failed to execute using user code

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

Additional Information: Types that inherit IEnumerable cannot be automatically mapped. Did you accidentally call GetRecord or WriteRecord, which act on one record, instead of calling GetRecords or WriteRecords, which act on the list of records?

GetRecords probably expects my own custom class, but I just want the values ​​to be both a primitive type and a string. In addition, I suspect that the entire string is converted to a single string, and not each value is a separate string.

+8
c # csv csvhelper
source share
4 answers

According to @Marc L, you can try the following:

 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; } 
+10
source share

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

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

http://joshclose.imtqy.com/CsvHelper/#reading-parsing

Update

Version 3 supports reading and writing IEnumerable properties.

+9
source share

Try it. It worked for me.

 TextReader reader = File.OpenText(filePath); CsvReader csvFile = new CsvReader(reader); csvFile.Configuration.HasHeaderRecord = true; csvFile.Read(); var records = csvFile.GetRecords<Server>().ToList(); 

A server is an entity class. This is how I created it.

  public class Server { private string details_Table0_ProductName; public string Details_Table0_ProductName { get { return details_Table0_ProductName; } set { this.details_Table0_ProductName = value; } } private string details_Table0_Version; public string Details_Table0_Version { get { return details_Table0_Version; } set { this.details_Table0_Version = value; } } } 
+3
source share

You are close. It's not that he is trying to convert a string to a string. CsvHelper tries to map each field in the string to properties of the type you give it, using the names specified in the title bar. In addition, he does not understand how to do this using IEnumerable types (which string implements), so he just throws it when it automatically matches this point when testing the type.


This is a difficult task for what you do. If your file format is simple enough, which seems to be a well-known field format, neither escaped nor quotation marks. I see no reason why you need to take the overhead of importing the library. You should be able to list values ​​as needed using System.IO.File.ReadLines() and String.Split() .

 //pseudo-code...you don't need CsvHelper for this IEnumerable<string> GetFields(string filepath) { foreach(string row in File.ReadLines(filepath)) { foreach(string field in row.Split(',')) yield return field; } } 
+2
source share

All Articles