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
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 "
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.
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.