There's a small problem with Steve's parser in that it doesn't handle
cases where the CSV field is quoted: "my stuff","some, or other stuff".
Here is an inelegant test file to work on a data file (named "data"
containing lines such as:
"STANCORP FINCL GR","SFG",52.00,0.00
"S&P 500 INDEX,RTH","^GSPC",1166.36,0.00
procedure main()
# "STANCORP FINCL GR","SFG",52.00,0.00
# "S&P 500 INDEX,RTH","^GSPC",1166.36,0.00
filein := open("data")
while line := read(filein) do {
cols := 0
line ? {
write("Line: " || line)
while p := find("\"," | ",") | 0 do {
cols +:= 1
write(cols || " " || tab(p))
tab(upto(',')) | tab(0) | fail
move(1)
if &pos >= *line then break
}
}
write("Time to read.")
}
end
-- Phillip
On Mon, 13 Oct 2008, Steve Wampler wrote:
>
> Bruce & Breeanna Rennie wrote:
>> My purpose is to split a two line CSV file into an appropriate CSV file
>> containing may lines. The original files can have anything ranging from
>> 30 odd values to near to 1000 values on each line.
>
> Is there something about the values that makes this hard (embedded
> separators, etc.)? Unless there is, it should take less time to
> write one than to find one. Here's a simple version:
>
> ---------------------------------------
> procedure main(args)
> every line := parseCSV(!&input) do {
> # Code to process array of values on each line
> }
> end
>
> # Produce a list of CSV values from a string.
> procedure parseCSV(s, sep)
> /sep := ","
> A := []
> s ? {
> while put(A, tab(upto(sep))) do move(1)
> put(A, tab(0)) # No separator after last field
> }
> return A
> end
> ---------------------------------------
>
> It generally doesn't take much longer to handle more
> complex cases (e.g. fields can be separated by spans
> of separators, fields can contain embedded separators,
> etc.), but those cases do require knowing more details
> of the format of the CSV file (how are embedded
> separators escaped, for example).
>
>
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group