Sample code for reading a CSV file

Bob Sneidar bobs at twft.com
Thu Feb 17 13:55:53 EST 2011


seems in that case you would have to check the text between every odd and even quote first and convert any cr's in between to another character before your line loop. It's not even that simple, because numbers and date values are not typically quoted are they? 

I'm beginning to see why some don't like csv files! 

Bob


On Feb 17, 2011, at 10:45 AM, Nonsanity wrote:

> I threw a quick one together, because little problems like this always
> intrigue me. :)
> 
> New stack, made two fields and a button, and put the source CSV list in the
> first. Button script as:
> 
> on mouseUp
>   put "" into o
>   put fld 1 into x
>   repeat with a = 1 to the number of lines in x
>      put line a of x into xx
>      set itemdel to quote
>      repeat with b = 1 to the number of items in xx
>         if trunc(b/2) = (b/2) then replace comma with numtochar(2) in item
> b of xx
>      end repeat
>      set itemdel to comma
>      repeat with b = 1 to the number of items in xx
>         get item b of xx
>         replace numtochar(2) with comma in it
>         put it & return after o
>      end repeat
>      put "--------------------" & return after o
>   end repeat
>   put o into fld 2
> end mouseUp
> 
> As long as quoted items don't have return charters in them, this should
> work. If that IS a possibility, it would get a tad more complicated.
> 
> ~ Chris Innanen
> ~ Nonsanity
> 
> 
> On Thu, Feb 17, 2011 at 1:16 PM, Paul Dupuis <paul at researchware.com> wrote:
> 
>> So I am tired of reinventing the proverbial wheel over and over again.
>> 
>> I have a new project that I want to read a CSV file for. Obviously reading
>> the file is easy. And is it was tab separated instead of comma, parsing out
>> the rows and columns is easy as well. However, with comma seperated data
>> where some columns contain string with commas in then that are encapsulated
>> in quotes, just plowing through the itemDelimiter and lineDelimter doesn't
>> work.
>> 
>> So before I bother to write code to handle encapsulated CSV data, I thought
>> I'd ask if anyone on the use-list has existing code to handle CSV's that
>> they'd be willing to share.
>> 
>> --
>> Paul Dupuis
>> Cofounder
>> Researchware, Inc.
>> http://www.researchware.com/
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list