Discussion

Escaping comma in CSV File

Hi,

I am creating new CSV file from the data which is there in clipboard. But if we have comma in the data then rest of the part of data gets shifted to next cell in spreadsheet as comma is a delimiter for CSV file is there any way to escape comma? I am using below code.

 

String strFileData =  getParameterValue("pyFileData");//getPrimaryPage().getValue("pySrc");
String ExcelFileNm = getParameterValue("ExcelFileName") + ".csv";
            
ClipboardPage objHeadersPage = tools.findPage("pxRequestor");
{
    objHeadersPage.putString(".pyHTTPResponseHeaders.contentType" ,"application/vnd.ms-excel");
    objHeadersPage.putString(".pyHTTPResponseHeaders.ContentDisposition", "attachment;filename="+ExcelFileNm);
    sendData("", strFileData);
}

**Moderation Team has archived post**

This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.

Comments

Keep up to date on this post and subscribe to comments

September 18, 2015 - 6:39pm

Looking at the date when this question was posted, I think you would have already found an answer for this. But, I'm just posting an option in case if it would help someone else. Let us know if you have come across any different options.

This is a common issue with applications which process CSV files. In most of the cases, enclosing your value within double quotes solves the problem. This seems to be a bit different in Pega - in the Parse-Delimited rule, there is an option to specify the Delimiter and Escape Character. If you have used "Custom Definition" and "Comma" as delimiter, by default, the escape character would be set as "\". So, you need to make sure the comma in your CSV field value is preceded by this escape character i.e. "\," and this would do the trick. It would be better if you can handle this in the source program that creates CSV files for Pega.