[REBOL] Reading "CSV" data files
From: joel::neely::fedex::com at: 17-Sep-2000 23:27
Below is what I had coded for translating "CSV" files into blocks.
Gabriele's feature of supplying the field delimiter was a nice touch,
but since CSV is so peculiar anyway, I just special-cased for comma.
HOWEVER, I still think that the behavior of parse/all is buggy. The
docs say that /all means that all characters are handled simply as
data. Therefore:
>> parse/all {"this","is","a ""big"" test!"} ","
== ["this" "is" "a " "big" " test!"]
>> foreach x parse/all {"this","is","a ""big"" test!"} "," [
[ print x]
this
is
a
big
test!
>>
appears broken to me. Shouldn't the output be a block of just THREE
strings:
["this" "is" {a ""big"" test!}]
this
is
a ""big"" test!
instead?
-jn-
==================================================================
REBOL []
readcsv: make object! [
all-records: copy []
one-record: copy []
one-segment: copy ""
one-field: copy ""
noncomma: complement charset ","
nonquote: complement charset {"}
segment: [
copy one-segment any nonquote
(if found? one-segment [append one-field one-segment])
]
quoted: [
{"} (one-field: copy "")
segment
any [{""} (append one-field {"}) segment]
{"}
]
unquoted: [copy one-field any noncomma]
field: [[quoted | unquoted] (append one-record one-field)]
record: [field any ["," field]]
run: func [f [file!] /local line] [
all-records: copy []
foreach line read/lines f [
one-record: copy []
either parse/all line record [
append/only all-records one-record
][
print ["parse failed:" line]
]
]
all-records
]
]