Sample code for reading a CSV file

Nonsanity form at nonsanity.com
Thu Feb 17 12:45:59 CST 2011


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
>


More information about the use-livecode mailing list