reading CSV text file
Ken Ray
kray at sonsothunder.com
Fri Jun 22 23:21:43 EDT 2007
On Fri, 22 Jun 2007 21:36:21 +0000, runrev260805 at m-r-d.de wrote:
> Hi,
>
> i have to read a textfile, which is comma separated and contains " as
> text identifier. The item delimiter is ,
> My problem is, i do not know, how can i read the file and detect each
> item of a line, as the items are sepearated by comma and the
> textfield/items could contain comma, too. How can i tell Revolution
> to ignore the comma in the "tesxtarea of an item"
Others have commented on how terrible CSV is, so I won't add my own
loathing of the format. However, if you MUST deal with CSV, Alex
Tweedly and Richard Gaskin came up with a reasonably speedy
implementation a few years back the converts CSV data to tab-delimited
format (which you can then parse at your leisure):
function CSV2Tab3 pData
local tNuData -- contains tabbed copy of data
local tReturnPlaceholder -- replaces cr in field data to avoid line
breaks which would be
-- misread as records; replaced
later during display
local tEscapedQuotePlaceholder -- used for keeping track of quotes
in data
local tInQuotedText -- flag set while reading data between quotes
--
put numtochar(11) into tReturnPlaceholder -- vertical tab as
placeholder
put numtochar(2) into tEscapedQuotePlaceholder -- used to simplify
distinction between
--
quotes in data and those in delimiters
--
-- Normalize line endings:
replace crlf with cr in pData -- Win to UNIX
replace numtochar(13) with cr in pData -- Mac to UNIX
--
-- Put placeholder in escaped quote (non-delimiter) chars:
replace ("\""e) with tEscapedQuotePlaceholder in pData
replace quote"e with tEscapedQuotePlaceholder in pData --<NEW
--
put space before pData -- to avoid ambiguity of starting context
split pData by quote
put False into tInsideQuoted
put 0 into tCounter
repeat for each line tKey in (the keys of pData)
add 1 to tCounter
put pData[tCounter] into k
if (tInsideQuoted) then
replace cr with tReturnPlaceholder in k
put k after tNuData
put False into tInsideQuoted
else
replace comma with tab in k
put k after tNuData
put true into tInsideQuoted
end if
end repeat
--
delete char 1 of tNuData -- remove the leading space
replace tEscapedQuotePlaceholder with quote in tNuData
return tNuData
end CSV2Tab3
Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/
More information about the use-livecode
mailing list