Add Quote marks around commas in a text file with code
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Add Quote marks around commas in a text file with code
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
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
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:
Try it right in the message box.
Now try
Craig
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
Now try
Code: Select all
answer quote & "," & quote
Last edited by dunbarx on Fri Jul 26, 2024 1:52 pm, edited 2 times in total.
Re: Add Quote marks around commas in a text file with code
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.
You should still think about what data you are creating and make basic checks for validity.
-
- 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
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!
...now with 20% less chalk dust!
Re: Add Quote marks around commas in a text file with code
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?
Craig
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?

Craig
Re: Add Quote marks around commas in a text file with code
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!
https://www.fourthworld.com/embassy/art ... t-die.html
I love the headline!

Re: Add Quote marks around commas in a text file with code
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
Craig
Re: Add Quote marks around commas in a text file with code
Sparkout.
Craig
I read as if that was out of his control.The data gets saved in a CSV file then imported to excel or numbers
Craig
Re: Add Quote marks around commas in a text file with code
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
OK.
And yes, if he has fist crack at user data, tabs, dammit.
Craig
And yes, if he has fist crack at user data, tabs, dammit.
Craig