CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Location
    .NET 4.0
    Posts
    4

    Convert csv to pipe delimited text

    Hi

    I have been looking around and can only see how to convert text to CSV.

    Basically i have a single line, quote qualified csv file that i want to turn into a standard pipe delimited text file

    so

    "545454","yes","yes","no","boo, hoo"

    to

    545454|yes|yes|no|boo, hoo

    Any ideas?

    Thank you

    .NET 4.0 with VS2010 Pro

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Convert csv to pipe delimited text

    If there aren't any spaces between the quotes and the comma (i.e ","), you could load up the file in a StringBuilder and then call:
    Code:
    sb.Replace("\".\"", "|"); // replace "." with |
    sb.Replace("\"", ""); // remove the leading and trailing "

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Convert csv to pipe delimited text

    Of course you can run into issues if your data may contain quotes as part of the data.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Dec 2005
    Posts
    114

    Re: Convert csv to pipe delimited text

    Converting a CSV file into a pipe delineated file should be simple and easy. The way I would approach is to write a lexer and parser for the csv file, process it into records, and then output those records into the format of your choice. For the lexer, you can have a class which iterates through all the characters in the file and keeps output tokens for the commas, the quotes, the text, and anything else that might pertain to csv format. I would also write a parser into which would be inputted the tokens, and this would verify the syntax and generate the records for the csv file. Outputting into a new format is a simple as writing the file. I might not be the wisest guy but I think that a simple search on lexing and recursive descent parsing will provide enough knowledge to achieve your goal.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured