>I'm working with some legacy data in (ugh!) CSV format (comma-separated
>values).
>
>Anyone have a quick-n-dirty method if turning that into the more sane
>tab-delimited format?

If your data is strictly comma separated rather than using quotes as 
well as commas, then it's dead easy:

        put "C:/somefile" into csvFile
        put url ("file:" & csvFile) into theData

        replace comma with tab in theData

        put theData into url ("file:" & tabFile)

This works with files of quite a substantial size.  If you don't want 
to read whole files into memory in this way, simply crab chunks of 
lines into a buffer, convert the commas to tabs in the buffer and 
write it out again.

If all fields are enclosed in quotes, then life gets a bit harder as 
you have to remove the first and last character from each line and 
then treat the remaining "," (quote+comma+quote) as field separators:

        put "C:/somefile" into csvFile
        put url ("file:" & csvFile) into theData

        -- remove first & last quotes:
        delete first char of theData
        delete last char of theData

        -- strip off quote at end of line and quote at start of line:
        put quote & return & quote into eolSep
        replace eolSep with return in theData

        -- replace "," field separators with tab:
        put quote & comma & quote into fieldSep
        replace fieldSep with tab in theData

        put theData into url ("file:" & tabFile)

If the data has come from something like Excel you hav more problems 
as it doesn't use quotes for all fields, only those with embedded 
commas.  This means you'll have to parse each line fully to track the 
first and second use of quotes etc.  I have done this as well in the 
past, but I don't have a quick solution to offer here unfortunately!

Cheers
Peter

--------------------------------------------------------
Peter Reid
Reid-IT Limited, Loughborough, Leics., UK
Tel: +44 (0)1509 268843 Fax: +44 (0)870 052 7576
E-mail: [EMAIL PROTECTED]
Web: http://www.reidit.co.uk

Archives: http://www.mail-archive.com/metacard%40lists.best.com/
Info: http://www.xworlds.com/metacard/mailinglist.htm
Please send bug reports to <[EMAIL PROTECTED]>, not this list.

Reply via email to