Archived Forum Post

Index of archived forum posts

Question:

force quoted text in csv

Aug 14 '17 at 20:32

Is there a way in the csv module to force a string to be quoted even though the text is valid? I have an issue where the system that I am sending data to is dropping the leading 0. If I send 01234 in the csv file, they see 1234.


Answer

Yes, just add double-quote chars escaped. For example, in C++:

    CkCsv csv;

csv.put_HasColumnNames(true);

csv.SetColumnName(0,"year");
    csv.SetColumnName(1,"color");
    csv.SetColumnName(2,"country");
    csv.SetColumnName(3,"food");

csv.SetCell(0,0,"2001");
    csv.SetCell(0,1,"red");
    csv.SetCell(0,2,"France");
    csv.SetCell(0,3,"cheese");

csv.SetCell(1,0,"2005");
    csv.SetCell(1,1,"blue");
    csv.SetCell(1,2,"United\r\nStates");
    csv.SetCell(1,3,"\"hamburger\"");

//  Write the CSV to a string and display:
    const char *csvDoc = csv.saveToString();
    printf("%s\n",csvDoc);