Page 1 of 1

Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 1:05 am
by trags3
I am working on an App that takes input from a user and saves data in a csv file.
The fields don't require commas but users might put them in some of the fields such as Address data.
I don't want to require the user to not use commas. The data gets saved in a CSV file then imported to excel or numbers. If commas are included in the input data they need quotation marks around any commas in order maintain the integrity of the CSV data and include the commas in the final excel file.
HENCE MY QUESTION
How can I write the code to add quotation marks around the commas in the data, then save the data to a CSV file, then import the CSV file to excel(windows) or numbers (mac)?
Thanks for the help.

Tom Ragsdale

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 1:43 am
by dunbarx
Hi.

Quote is a constant in liveCode. Quotes normally delimit a literal, as in "chicken". So you have to have the constant in order to:

Code: Select all

answer "chcken" && quote & "chcken" & quote
Try it right in the message box.
Now try

Code: Select all

answer quote & "," & quote
Craig

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 7:43 am
by SparkOut
Use tab instead of comma as your separator before export. Excel will natively import a tab separated file.

You should still think about what data you are creating and make basic checks for validity.

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 8:27 am
by scott_morrow

Code: Select all

put (QUOTE & COMMA & QUOTE) into tEscapedComma
replace COMMA with tEscapedComma in tUserData

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 1:55 pm
by dunbarx
Sparkout.

The OP said that CSV (ugh) was the format required. I never use that, for reasons that have been exhaustively discussed in the forum here.

Tom.

You see the use of another constant, "comma", that can be used in place of ",". So maybe that obviates the need to use the constant "quote".

See how much fun you can have with LC syntax? :D

Craig

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 3:58 pm
by Klaus
Here a great handler which covers almost everything with QUOTES in a CSV file:
https://www.fourthworld.com/embassy/art ... t-die.html

I love the headline! :-D

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 4:16 pm
by dunbarx
I hate CSV. The only way to beat it in stupidity is to advocate for the use of the char "e" as a delimiter.

Craig

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 7:25 pm
by SparkOut
dunbarx wrote:
Fri Jul 26, 2024 1:55 pm
The OP said that CSV (ugh) was the format required.
No, the OP said that is what currently happens. I say change what happens. We all know why.

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 7:26 pm
by SparkOut
dunbarx wrote:
Fri Jul 26, 2024 4:16 pm
I hate CSV. The only way to beat it in stupidity is to advocate for the use of the char "e" as a delimiter.

Craig
superbly put!

Re: Add Quote marks around commas in a text file with code

Posted: Fri Jul 26, 2024 9:13 pm
by dunbarx
Sparkout.
The data gets saved in a CSV file then imported to excel or numbers
I read as if that was out of his control.

Craig

Re: Add Quote marks around commas in a text file with code

Posted: Sat Jul 27, 2024 9:28 am
by SparkOut
Mmm, I read as he's getting users to input data into the app he's working on from where he saves it. Sounds like he has control, and wants to know how to format csv data with enclosing quotes for each data item. Which is the wrong way to do this if it can be avoided. I think we agree on that.

Re: Add Quote marks around commas in a text file with code

Posted: Sat Jul 27, 2024 6:57 pm
by dunbarx
OK.

And yes, if he has fist crack at user data, tabs, dammit.

Craig