How to parse a CSV file using Regular Expressions (FAQ)

Last post 03-28-2007 3:57 AM by KBrocksi_SEC. 5 replies.

Sort Posts:

  • How to parse a CSV file using Regular Expressions (FAQ)

    02-11-2004, 3:36 PM
    • Member
      660 point Member
    • hotblue
    • Member since 10-14-2002, 9:56 AM
    • Makati City, Philippines
    • Posts 132
    Sometimes, you find the need to parse Comma-Separated Values or CSV files dynamically into a DataTable. CSV files can be straight forward or complex. Complexity comes in when the data itself has embedded commas, quotes or line breaks. This solution, written in C# and using the .NET framework's Regular Expression object can handle such complex formatting and has been tested to work with CSV formatted files saved from Microsoft Excel.
    Willeus Acuña :)

    Visit hotblue.com - tons of advice on .NET programming!

    Comprehensive .NET directory with 8,000+ articles indexed.
  • Re: How to parse a CSV file using Regular Expressions (FAQ)

    01-16-2007, 10:48 AM
    • Member
      94 point Member
    • Fyzbo
    • Member since 05-24-2006, 9:24 PM
    • Posts 34

    I was searching for a solution like this one.  Unfortunately I found an error in this solution.  If you give it a csv missing a column in one row it messes up. i.e

    1,2,3
    1,2,3
    1,,3
    1,2,3

     It will produce a datatable of:

    1,2,3
    1,2,3
    1,3,
    1,2,3
     

  • Re: How to parse a CSV file using Regular Expressions (FAQ)

    01-23-2007, 11:24 PM
    • Contributor
      5,452 point Contributor
    • CSharpSean
    • Member since 10-21-2006, 5:43 AM
    • Orlando, FL
    • Posts 915
    you could insert some text "null" or "noData" and when parsing through the cvs file, look for this text and do what you need it with the other data.
  • Re: How to parse a CSV file using Regular Expressions (FAQ)

    03-07-2007, 8:44 PM
    • Member
      44 point Member
    • scorpion4000
    • Member since 08-13-2006, 11:13 PM
    • Austria (Salzburg)
    • Posts 13

    you can also read an csv-table with ado.net or odbc.

    example here:

    http://www.codeproject.com/cs/database/FinalCSVReader.asp

     

     

  • Re: How to parse a CSV file using Regular Expressions (FAQ)

    03-28-2007, 3:53 AM
    • Contributor
      3,382 point Contributor
    • KBrocksi_SEC
    • Member since 03-20-2003, 1:39 AM
    • Düsseldorf, Germany
    • Posts 627

    Hi,

    in my mind the easiest way is to use the -of course free- "FileHelpers Library" from Marcos Meli. This is an automatic file import/export framework. This library is an easy to use .NET library to read/write strong typed data from files with fixed length or delimited records (CSV). Also has support to import/export data from different data storages (Excel, Acces, SqlServer).

     FileHelpers Home Page at SourceForge  (Project Summary)
     FileHelpers Discussions Forums 

    The main idea is pretty simple:

    You can strong type your flat file (fixed or delimited) simply describing a class that maps to each record of the file and work with your file like a strong typed .NET array.

    Regards,

    Karsten
  • Re: How to parse a CSV file using Regular Expressions (FAQ)

    03-28-2007, 3:57 AM
    • Contributor
      3,382 point Contributor
    • KBrocksi_SEC
    • Member since 03-20-2003, 1:39 AM
    • Düsseldorf, Germany
    • Posts 627

    Hi,

    in my mind the easiest way is to use the -of course free- "FileHelpers Library" from Marcos Meli. This is an automatic file import/export framework. This library is an easy to use .NET library to read/write strong typed data from files with fixed length or delimited records (CSV). Also has support to import/export data from different data storages (Excel, Acces, SqlServer).

     FileHelpers Home Page at SourceForge  (Project Summary)
     FileHelpers Discussions Forums 

    The main idea is pretty simple:

    You can strong type your flat file (fixed or delimited) simply describing a class that maps to each record of the file and work with your file like a strong typed .NET array.

    Regards,

    Karsten
    Filed under:
Page 1 of 1 (6 items)