text files with quotes

BCE mark at bcesouth.com
Wed May 15 19:54:01 EDT 2002


David,

Thanks for the response  :-)

A sample line in a file I might want to parse:
1,"5/14/02, 10:00 AM",Pending,Batch0001,"This is a memo field, gee it's
cool",45362

This line is comma delimited, but if I try to separate it that way, the
interior quoted commas will mess things up, so I wrote a routine to fix
that.

The code is pasted below.  It first sets the item delimiter to a quote, and
gets rid of any commas inside quotes, then sends the routine on to another
function to replace the comma delimiters with tabs (or whatever).  It uses
"mod" to check for "0" remainders, thus it can distinguish where to strip
out quoted commas, as the items delimited by quotes will be divisible by 2.
This was the best method I could figure at the time.  It also posts what
line number it is working on in a status field.

Incidentally, if ALL fields were surrounded by quotes, I could easily do a
replace using
replace (quote & "," & quote) with tab in field "myfile"

In fact, I tested it, and it parses the file very quickly, but since only a
few items are quoted, it's not the right tool.


The code:


on mouseup
  set the itemdelimiter to quote
  answer the number of items in line 1 of field "myfile"
  put 1 into x
  repeat for the number of lines of field "myfile"
  put 1 into n

  repeat for the number of items in line x of field "myfile"
    if n mod 2=0 then
      replace "," with " " in item n in line x of field "myfile"
      end if
    add 1 to n
  end repeat

  put "Parsing record " & x & "..." into field "status"
  add 1 to x
end repeat
send mouseup to button "r2"
end mouseup

Thanks again for any ideas on this.

Mark



----- Original Message -----
From: "David Vaughan" <drvaughan55 at mac.com>
To: <use-revolution at lists.runrev.com>
Sent: Wednesday, May 15, 2002 7:46 PM
Subject: Re: text files with quotes


>
> On Thursday, May 16, 2002, at 04:56 , Mark Paris wrote:
>
> > Anyone have a good method of parsing a text file that is delimmited by
> > commas, yet has quotes (") surrounding SOME of the records, which in
> > turn
> > may have commas inside of them (such as a notes field)?  (!)
> >
> > The only routine I made for this was long and ugly, taking a second per
> > record.  I first replaced commas inside of quotes with a blank space,
> > then
> > got rid of the quotes, then parsed by comma.
>
> Mark
>
> I am not sure there is a trivial way of doing this but your second per
> record sounds vastly too long to me. Rev is very fast at text processing
> in my experience, after a little familiarity with its nifty features.
> Are you able to post a sample of your data and your script (or a
> fragment) to handle it? I have some thoughts about it but need some more
> material to be sure of what might work well. I am prepared to bet that,
> if not me, then someone will pounce with an improvement for you.
>
> regards
> David
> >
> > Is there a routine I might be missing?  Thanks!
> >
> > Mark
> >
> >
> > _______________________________________________
> > use-revolution mailing list
> > use-revolution at lists.runrev.com
> > http://lists.runrev.com/mailman/listinfo/use-revolution
> >
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list