0

I would use FileHelpers to import a file like this,

STATUS,USERID,CUSTOM1,CUSTOM2
Active,000001,"Company division "A"", "HRO"
Active,000002,"Company HQ", "HRO"

but I can not find how to specify a field delimiter which in turn may contain the delimiter itself, can anyone help?

DaniloZ
  • 49
  • 4
  • That's what quotes are for. In quoted text field delimiter should be ignored (and quotes has to be escaped). Strictly speaking you need quotes only if: 1) text contains delimiter or 2) text contains newline. Note that using \t instead of comma will reduce chances you have delimiter inside your text. – Adriano Repetti Feb 17 '15 at 15:21
  • 1
    Possible duplicate of http://stackoverflow.com/questions/21134981/filehelpers-nested-quotes-and-commas-parsing-error – publicgk Sep 15 '15 at 07:16

2 Answers2

0

In another thread somebody suggested using the (probably rather new) Microsoft.VisualBasic.FileIO.TextFieldParser (see FileHelpers: How to handle quoted fields when reading file). I had the same problem consuming files generated by Magento, so I tried the class, and it correctly handles the format mentioned above (doublequotes inside a quoted textual field).

As a C# user, don't worry about VB, just reference the standard Microsoft.VisualBasic assembly.
And by the way I disagree that the format in question is invalid, as another user suggests. On the contrary I've seen it many times in my job, and as far as I know there is no CSV-Format-Standard as such. In the end you just have to deal with what comes out of some piece of software... ;-)

Community
  • 1
  • 1
PMue
  • 1
  • 2
-1

You should probably take a look at CSVHelper which will help you do this

krystan honour
  • 6,523
  • 3
  • 36
  • 63
  • Great work, although there is still a little problem because the field [Company division "A"] is imported as [Company division A""], but at least divides them properly. Thanks – DaniloZ Feb 17 '15 at 16:29
  • I'd like to know who downvoted this when the original guy said this helped him... – krystan honour Feb 17 '15 at 16:55
  • It was probably downvoted for the fact that a completely different engine was suggested over the FileHelpers one. However, there is no easy way to do this in FileHelpers aside from manual parsing on the BeforeRead or INotifyRead events. The layout above is an invalid format and the source has created the file incorrectly, but it's doubtful you can change the source. – netniV Aug 09 '15 at 19:14