Giannis Paraskevopoulos
Giannis Paraskevopoulos

Reputation: 18411

Trim value before reading

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.

Upvotes: 3

Views: 1973

Answers (3)

Giannis Paraskevopoulos
Giannis Paraskevopoulos

Reputation: 18411

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; }
}

Upvotes: 4

Steve
Steve

Reputation: 216352

I was able to solve the problem without changing the int declaration to a double using the TypeConverter

Here the steps:

First create the type converter class

public class TestClass
{
    public int Value { get; set; }
}
public class IntegerWithGroupSeparatorConverter: CsvHelper.TypeConversion.ITypeConverter
{
    public string ConvertToString(object value, IWriterRow row, MemberMapData mpd)
    {
        return value?.ToString();
    }
    public object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
    {
        if(Int32.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out int result))
            return result;
        else
            return 0; // Not sure if 0 is acceptable or not
    }
}
public class TestMap : ClassMap<Test>
{
    public TestMap()
    {
        Map(m => m.Value).TypeConverter<IntegerWithGroupSeparatorConverter>();
    }
}

Now we can supply this type converter to your reading code with this

List<TestClass> entries;
CultureInfo ci = new CultureInfo("en-us");
using (var reader = new StringReader(csvContent))
using (var csvReader = new CsvReader(reader, ci))
{
    csvReader.Context.RegisterClassMap<TestMap>();
    entries = csvReader.GetRecords<TestClass>().ToList();
}

Upvotes: 0

Oliver
Oliver

Reputation: 45119

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; }
}

Upvotes: 2

Related Questions