Add Quote marks around commas in a text file with code

Want to talk about something that isn't covered by another category?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Add Quote marks around commas in a text file with code

Post by trags3 » Fri Jul 26, 2024 1:05 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Jul 26, 2024 1:43 am

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
Last edited by dunbarx on Fri Jul 26, 2024 1:52 pm, edited 2 times in total.

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Fri Jul 26, 2024 7:43 am

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.

scott_morrow
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 38
Joined: Tue Jun 27, 2006 8:35 pm
Contact:

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

Post by scott_morrow » Fri Jul 26, 2024 8:27 am

Code: Select all

put (QUOTE & COMMA & QUOTE) into tEscapedComma
replace COMMA with tEscapedComma in tUserData
Last edited by scott_morrow on Fri Jul 26, 2024 11:22 pm, edited 1 time in total.
Elementary Software
...now with 20% less chalk dust!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Jul 26, 2024 1:55 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus » Fri Jul 26, 2024 3:58 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » 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

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Fri Jul 26, 2024 7:25 pm

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.

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Fri Jul 26, 2024 7:26 pm

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!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Jul 26, 2024 9:13 pm

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

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Sat Jul 27, 2024 9:28 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Sat Jul 27, 2024 6:57 pm

OK.

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

Craig

Post Reply