I have to create a CSV file from webservice output and the CSV file uses quoted strings with comma separator. I cannot change the format...
So if I have a string
it becomes a "string"
... If the value has quotes already they are replaced with double quotes. For example a str"ing
becomes "str""ing"
...
However, lately my import has been failing because of the following
"","word1,word2,..."
"""",""word1,word2,...""
""""",""word1,word2,..."""
As you can see the final result is this:
""""",""word1,word2,..."""
which breaks my import (is sees it as another field)... I think the issue is appereance of ","
in the original input string.
Is there a CVS escape sequence for this scenario?
The reason why above breaks is due to BCP mapping file (BCP utility is used to load CSV file into SQL db) which has terminator defined as ","
. So instead of seeing 1 field it sees 2...But I cannot change the mapping file...
By default, the escape character is a " (double quote) for CSV-formatted files. If you want to use a different escape character, use the ESCAPE clause of COPY , CREATE EXTERNAL TABLE or gpload to declare a different escape character.
Double-quote escape characters There are 2 accepted ways of escaping double-quotes in a CSV file. One is using a 2 consecutive double-quotes to denote 1 literal double-quote in the data. The alternative is using a backslash and a single double-quote.
I use this code and it has always worked:
/// <summary> /// Turn a string into a CSV cell output /// </summary> /// <param name="str">String to output</param> /// <returns>The CSV cell formatted string</returns> public static string StringToCSVCell(string str) { bool mustQuote = (str.Contains(",") || str.Contains("\"") || str.Contains("\r") || str.Contains("\n")); if (mustQuote) { StringBuilder sb = new StringBuilder(); sb.Append("\""); foreach (char nextChar in str) { sb.Append(nextChar); if (nextChar == '"') sb.Append("\""); } sb.Append("\""); return sb.ToString(); } return str; }
Based on Ed Bayiates' answer:
/// <summary> /// Turn a string into a CSV cell output /// </summary> /// <param name="value">String to output</param> /// <returns>The CSV cell formatted string</returns> private string ConvertToCsvCell(string value) { var mustQuote = value.Any(x => x == ',' || x == '\"' || x == '\r' || x == '\n'); if (!mustQuote) { return value; } value = value.Replace("\"", "\"\""); return string.Format("\"{0}\"", value); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With