I have a csv i need to read that in a column it contains an integer value, but there is a space after the value, so the in the csv the value is "1,264 "
.
In the class i have
public int MyValue { get; set; }
but when i try to ReadRecords i get the following error:
TypeConverterException: The conversion cannot be performed. Text: ‘1,264 ‘ MemberType: System.Int32 TypeConverter: ‘CsvHelper.TypeConversion.Int32Converter’ IReader state: ColumnCount: 0 CurrentIndex: 26 HeaderRecord:
Apparently i need to trim the empty space in the field. I have no control over the format of the initial format.
I could make it a string property and parse it at a second pass, but i want to see if there is another way.
UPDATE
I tried removing the space and still get the error. I had thought this might have been a culture info issue, and when i am reading i have the following code:
using (var reader = new StreamReader(@"C:tempfile.csv")) using (var csv = new CsvReader(reader, new CsvConfiguration(new CultureInfo("en-US")) { TrimOptions = TrimOptions.Trim })) {...}
UPDATE 2
Consider a file containing a header with a single column and a single row (besides the header):
MyValue "1,264 "
UPDATE 3
My current locale dictates that the comma is a decimal separator and the point is the thousands separator.
Answer
So, as already mentioned in the comments 1.264
is a double
and trying to parse it as an integer will throw an exception. And the additional space is not a problem in a default CSV configuration:
public static class Program { static void Main(string[] args) { var csvContent = @"Id,Value 1,""1.236 """; List<Entry> entries; using (var reader = new StringReader(csvContent)) using (var csvReader = new CsvReader(reader, CultureInfo.GetCultureInfo("en-us"))) { entries = csvReader.GetRecords<Entry>().ToList(); } foreach (var entry in entries) { Console.WriteLine($"{entry.Id} {entry.Value}"); } } } public class Entry { public int Id { get; set; } public double Value { get; set; } }