Search code examples
c#filehelpers

Removing quotes in file helpers


I have a .csv file(I have no control over the data) and for some reason it has everything in quotes.

"Date","Description","Original Description","Amount","Type","Category","Name","Labels","Notes"
"2/02/2012","ac","ac","515.00","a","b","","javascript://"
"2/02/2012","test","test","40.00","a","d","c",""," "

I am using filehelpers and I am wondering what the best way to remove all these quotes would be? Is there something that says "if I see quotes remove. If no quotes found do nothing"?

This messes with the data as I will have "\"515.00\"" with unneeded extra quotes(especially since I want in this case it to be a decimal not a string".

I am also not sure what the "javascript" is all about and why it was generated but this is from a service I have no control over.

edit this is how I consume the csv file.

    using (TextReader textReader = new StreamReader(stream))
        {
            engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; 

            object[] transactions = engine.ReadStream(textReader);
        }

Solution

  • You can use the FieldQuoted attribute described best on the attributes page here. Note that the attribute can be applied to any FileHelpers field (even if it type Decimal). (Remember that the FileHelpers class describes the spec for your import file.. So when you mark a Decimal field as FieldQuoted, you are saying in the file, this field will be quoted.)

    You can even specify whether or not the quotes are optional with

    [FieldQuoted('"', QuoteMode.OptionalForBoth)] 
    

    Here is a console application which works with your data:

    class Program
    {
        [DelimitedRecord(",")]
        [IgnoreFirst(1)]
        public class Format1
        {
            [FieldQuoted]
            [FieldConverter(ConverterKind.Date, "d/M/yyyy")]
            public DateTime Date;
            [FieldQuoted]
            public string Description;
            [FieldQuoted]
            public string OriginalDescription;
            [FieldQuoted]
            public Decimal Amount;
            [FieldQuoted]
            public string Type;
            [FieldQuoted]
            public string Category;
            [FieldQuoted]
            public string Name;
            [FieldQuoted]
            public string Labels;
            [FieldQuoted]
            [FieldOptional]
            public string Notes;
        }
    
        static void Main(string[] args)
        {
            var engine = new FileHelperEngine(typeof(Format1));
    
            // read in the data   
            object[] importedObjects = engine.ReadString(@"""Date"",""Description"",""Original Description"",""Amount"",""Type"",""Category"",""Name"",""Labels"",""Notes""
    ""2/02/2012"",""ac"",""ac"",""515.00"",""a"",""b"","""",""javascript://""
    ""2/02/2012"",""test"",""test"",""40.00"",""a"",""d"",""c"","""","" """);
    
            // check that 2 records were imported
            Assert.AreEqual(2, importedObjects.Length);
    
            // check the values for the first record
            Format1 customer1 = (Format1)importedObjects[0];
            Assert.AreEqual(DateTime.Parse("2/02/2012"), customer1.Date);
            Assert.AreEqual("ac", customer1.Description);
            Assert.AreEqual("ac", customer1.OriginalDescription);
            Assert.AreEqual(515.00, customer1.Amount);
            Assert.AreEqual("a", customer1.Type);
            Assert.AreEqual("b", customer1.Category);
            Assert.AreEqual("", customer1.Name);
            Assert.AreEqual("javascript://", customer1.Labels);
            Assert.AreEqual("", customer1.Notes);
    
            // check the values for the second record
            Format1 customer2 = (Format1)importedObjects[1];
            Assert.AreEqual(DateTime.Parse("2/02/2012"), customer2.Date);
            Assert.AreEqual("test", customer2.Description);
            Assert.AreEqual("test", customer2.OriginalDescription);
            Assert.AreEqual(40.00, customer2.Amount);
            Assert.AreEqual("a", customer2.Type);
            Assert.AreEqual("d", customer2.Category);
            Assert.AreEqual("c", customer2.Name);
            Assert.AreEqual("", customer2.Labels);
            Assert.AreEqual(" ", customer2.Notes);
        }
    }
    

    (Note, your first line of data seems to have 8 fields instead of 9, so I marked the Notes field with FieldOptional).