Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim value before reading

Tags:

c#

csvhelper

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

The code

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.

like image 497
Giannis Paraskevopoulos Avatar asked May 08 '26 15:05

Giannis Paraskevopoulos


2 Answers

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; }
}
like image 134
Giannis Paraskevopoulos Avatar answered May 10 '26 04:05

Giannis Paraskevopoulos


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; }
}
like image 45
Oliver Avatar answered May 10 '26 05:05

Oliver