4

I am currently using the FileHelpers library (v2.0.0.0) to parse a CSV file. The CSV file is mapped to a class that has a handful of public properties, let's say there are N. The problem is that, by default, FileHelpers doesn't seem to correctly handle cases where the user specifies a CSV file that has more than N-1 commas. The remaining commas just get appended to the last property value.

I figured this must be configurable via FileHelpers' attributes, but I didn't see anything that would ignore fields that don't have a matching property in the record.

I looked into the RecordConditions, but using something like ExcludeIfEnds(",") looks like it will skip the line entirely if it ends with a comma, but I just want them stripped.

It's possible that my only recourse is to pre-process the file and strip any trailing commas, which is totally fine, but I wanted to know if FileHelpers can do this as well, and perhaps I'm just not seeing it in the docs.

Dave
  • 14,618
  • 13
  • 91
  • 145

3 Answers3

4

Just an idea for a hack / workaround: you could create a property called "ExtraCommas" and add it to your class, so that extra commas are serialized there and not in the real properties of your object...

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • I like that, and hadn't even thought of it. I'll give it a try. – Dave Jul 06 '12 at 14:23
  • You should follow @Dave but also add the `[FieldOptional]` attribute. See the [documentation](http://www.filehelpers.com/FileHelpers.FieldOptionalAttribute.html) to your extra field. – shamp00 Jul 09 '12 at 08:03
0

If the number of commas varies, I think you are out of luck and would have to do post processing. However you can set blank fields in your class if there are a fixed amount.

[FieldOrder(5)] 
public string Blank1;

[FieldOrder(6)]
public string Blank2;

This doesn't really ever bite me because I don't use a FileHelpers class as a business class, I use it as an object to build the business class from. I store it for auditing. I think at one point I played around with making the fields for the Blanks private, not sure how that turned out.

Jeremy Gray
  • 1,378
  • 1
  • 9
  • 24
  • Thanks for the suggestion. Unfortunately, FileHelpers 2.0 doesn't have this attribute, and that's the version I am using. I will update my OP. – Dave Jul 06 '12 at 13:11
  • You don't need the FieldOrder, I just left it in there because it used to be a "best practice" (that I never cared for). My solution is to count the number of commas, then add a phantom fields at the end of your class called BlankN, BlankN+1. (wasn't aware that had gone away though, thanks!) – Jeremy Gray Jul 06 '12 at 13:14
0

Here is a custom method that you can use, it might not be the best solution, but it will solve the last comma problem. The code could be more optimized for sure, this is just to give you the idea of how to get around this kind of problem.

  int main(){
    StreamReader sr = new StreamReader(@"C:\Users\musab.shaheed\Desktop\csv.csv");
     var lineCount=File.ReadLines(@"C:\Users\musab.shaheed\Desktop\csv.csv").Count();
            for (int i = 0; i < lineCount;i++ ) {

            String fileText = sr.ReadLine();

            fileText=fileText.Substring(0, fileText.Length - 1);


            //store your data in here
            Console.WriteLine(fileText);


            };

            sr.Close();

}
musab shaheed
  • 150
  • 1
  • 7