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:\temp\file.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.
I dug into the source code and found the solution.
If you check the source for Int32Converter you may see, it converts using the NumberStyles defined in the member as an attribute or get the default of integer.
When I set the NumberStyles attribute to Number then it worked without any other change. I prefer this solution since I do not have to change the expected type of the property.
So I just had to change the class definition to:
public class TestClass
{
[Name("MyValue"), CsvHelper.Configuration.Attributes.Optional, NumberStyles(NumberStyles.Number)]
public int MyValue { get; set; }
}