csv files
[1/6] from: gchiu:compkarori at: 5-Jan-2001 22:31
Does anyone have a routine to parse csv files?
Bo's example in the Rebol script library doesn't take into
account the situation where a "field" contains commas, and
so is enclosed by quotation marks, and quotation marks
contained within such fields are escaped by a quotation
mark.
--
Graham Chiu
[2/6] from: al:bri:xtra at: 5-Jan-2001 22:56
> Does anyone have a routine to parse csv files?
Yes. But it's not at my home computer where I am. I won't have access to
this until Monday at the earliest.
Andrew Martin
ICQ: 26227169 http://members.nbci.com/AndrewMartin/
[3/6] from: al:bri:xtra at: 5-Jan-2001 22:57
Graham wrote:
> Bo's example in the Rebol script library doesn't take into account the
situation where a "field" contains commas, and so is enclosed by quotation
marks, and quotation marks contained within such fields are escaped by a
quotation mark.
Can you give an example of this last example, as it sounds a little
confusing to me.
Andrew Martin
ICQ: 26227169 http://members.nbci.com/AndrewMartin/
[4/6] from: gchiu:compkarori at: 5-Jan-2001 23:58
On Fri, 5 Jan 2001 22:57:32 +1300
"Andrew Martin" <[Al--Bri--xtra--co--nz]> wrote:
> > Bo's example in the Rebol script library doesn't take
> into account the
<<quoted lines omitted: 6>>
> sounds a little
> confusing to me.
Sure. A csv file looks like this
datum1, datum2, datum3 ...
but sometimes, like this
datum1,"this is data with commas, and quotes eg 14"" monitor
within the field",datum3 ...
Despite it being midnight, I managed to cobble something
together which seems to do the trick.
--
Graham Chiu
[5/6] from: g:santilli:tiscalinet:it at: 5-Jan-2001 20:11
Hello Graham!
On 05-Gen-01, you wrote:
GC> Does anyone have a routine to parse csv files?
You might want to try the following...
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
REBOL [
Title: "CSV parser"
Author: "Gabriele Santilli"
]
CSV-parser: make object! [
line-rule: [field any [separator field]]
field: [[quoted-string | string] (insert tail fields any [f-val copy ""])]
string: [copy f-val any str-char]
quoted-string: [{"} copy f-val any qstr-char {"} (replace/all f-val {""} {"})]
str-char: none
qstr-char: [{""} | separator | str-char]
fields: []
f-val: none
separator: #";"
set 'parse-csv-line func [
"Parses a CSV line (returns a block of strings)"
line [string!]
/with sep [char!] "The separator between fields"
] [
clear fields
separator: any [sep #";"]
str-char: complement charset join {"} separator
parse/all line line-rule
copy fields
]
]
[6/6] from: brett:codeconscious at: 6-Jan-2001 12:17
The one on this page is getting a bit long in the tooth now and could very
probably benefit from a lot of tidying but here you go anyway. It allows you
to import and export.
http://www.codeconscious.com/rebol/rebol-scripts.html#CommaandTabdelimitedfi
les
Brett.
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted