954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying comma in .csv fille

I am saving the output of my program in .csv file.
I need to save a variable's value in one column, but the value may contain comma.
When the program encounters a comma it places half of the variable's value in
the next column.

Is there any way through which I can avoid the placement of value in the next column..

saneeha
Newbie Poster
20 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Put the thing in quotes
1,"hello, world",2
would be 3 columns.

Use excel to save a simple csv format file with a field containing a comma.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

If double quotes are used it prints the variable name instead of value...

saneeha
Newbie Poster
20 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Or you could just post some actual code....

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Interesting that you want to use a format specifically meant for separating individual values with commas, but need to retain information with commas inside it.

A good way of doing this would be to make your own tokenizer and only parse information when a stack is empty.

When you encounter quotes, push it onto the stack and read characters contiguously, even if it is a comma.

When you encounter quotes again, pop the original quote off of the stack and let your custom tokenizer do its thing, like normal.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You