dimanche 7 juin 2015

Including comma's in a CSV file without delineation?

I have a database of which I am exporting each tables contents into a .csv file. Some of the text from my database contains commas. In some cases commas are needed to separate the columns in the .csv file but others are part of the text and I don't want them to have this affect.

How do I include commas in a .csv file without having them read as column separaters?

Is there any possible way of going about this. I know I can escape the comma but I don't want this escape character to appear in my .csv file. Is there any other approach to dealing with this?

I'm using an Sqlite D'base with C++.

I have tried the below and I'm still having some problems. If I want to add a string of words containing a comma to some text then I would do this. string x = "\"This is, a test\""; and this works fine. All the text goes into one cell in my .csv file read by excel with no delineation.

But, If I have two strings and want to insert a comma which will be read as a comma an not as a delineator in my .csv file then how do I do it.

Below I have used the 'comma' variable to hold a comma but and tried to insert this into variable c. No errors when building it but when that is inserted into a .csv file the comma is recognized as a delineator.

I have used many variations of the escape operator with no luck. Any clues as to how to go about this?

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

ofstream output;

int main()
{
   output.open("file.csv",ios::out |ios::binary);
   string comma = ",";
   string a = "\"This is, a test\"";
   string b = "This is, a test";
   string c = "test1"+ comma+"test2";

   output << a << endl << b << endl << c << endl;
}

Aucun commentaire:

Enregistrer un commentaire