CSV Processing

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ivanw
Posts: 6
Joined: Sat Jun 23, 2007 9:10 am

CSV Processing

Post by ivanw » Fri Jul 10, 2009 5:25 am

I'm hitting an issue when parsing csv data - searched the forum but could not find an answer.

Basically I'm doing a item loop to extract csv data.
However my csv data is itself separated by single quotes.

e.g. when I loop over this with a "for each item":

Code: Select all

11.66,    '201,000',    cat
I get:

Code: Select all

11.66
201
000
cat

Does anyone know if Rev has item processing so I can instead get this?

Code: Select all

11.66
201,000
cat

Thanks!


Ivan

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

CSV processing

Post by jmburnod » Fri Jul 10, 2009 7:55 am

Ivan,

You can change the itemdelimiter.


Jean-Marc

ivanw
Posts: 6
Joined: Sat Jun 23, 2007 9:10 am

Post by ivanw » Fri Jul 10, 2009 8:05 am

Hi Jean Marc,

Sorry I think I didn't explain clearly.

My data is actually comma delimited, but some items have a non-delimiting comma, and these items are identified by single quotes:

e.g. '100,000' is actually one item and not two as the comma should not be recognized as an item delimiter.

I was wondering if the item processing could take account of that.

I think I will have to code a workaround.


Thanks anyway,
Ivan

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Jul 10, 2009 8:44 am

Hi Ivan and Jean-Marc,

Try the following function:

Code: Select all

function csv2tsv theData,theRemoveQuote
     replace comma with tab in theData
     replace "'" with quote in theData
     put number of words of theData
     repeat for each line myLine in theData
          repeat for each word myWord in myLine
               if myWord contains tab then replace tab with comma in myWord
               put myWord & tab after myNewData
          end repeat
          delete last char of myNewData
          put cr after myNewData
     end repeat
     if theRemoveQuote is true then replace quote with empty in myNewData
     return myNewData
end csv2tsv
This function converts your comma-delimited data to tab-delimited data. If theRemoveQuote is true, it also removes the quotes.

Best regards,

Mark
Last edited by Mark on Fri Jul 10, 2009 12:49 pm, edited 2 times in total.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

CSV processing

Post by jmburnod » Fri Jul 10, 2009 9:42 am

Yes Mark

Your csv2tsv function work

Best regards


Jean-Marc

ivanw
Posts: 6
Joined: Sat Jun 23, 2007 9:10 am

Post by ivanw » Fri Jul 10, 2009 5:40 pm

Thank you Mark & Jean-Marc,

I've learnt something new about word processing and quotes in Rev.


Ivan

Post Reply