csv file reading
[1/2] from: jblake:arsenaldigital at: 31-Jan-2008 11:27
I have a Rebol utility that will read a csv file and open tickets with
the info.
Works like a champ. Or should I say "worked". The last field is a
comment field.
In the recent csv, the comment was actually a e-mail that had been put
in the field. Rebol normally reads line till the newline. Well the
e-mail has a bunch of newlines in it so if I look at the file on
NotePad, it sees all of them and shows new lines. This is causing Rebol
to think each line of the mail should be used to open a ticket which is
not going to work.
Has anyone come up with a way to deal with multiline fields in a csv?
For example,
You have 5 fields.
Customer,server,contact,phone,comment
The data is
Bill,Bilbo,bill-yahoo.com,555-555-5555, Please open a ticket for this
server
That is correct.
But if they put say 5 lines of text in the comment field, how do we
deal with that?
I'm currently using [,"] for delimiters because excel will auto put in
the " if it needs to.
Ideas?
I'm searching the archives for info.
Thanks
John
[2/2] from: christian::ensel::gmx::de at: 31-Jan-2008 23:01
Hi John,
/first/, despite not knowing the origin and format restrictions of the
emails you have to deal with, I suspect that commas probably aren't the
perfect choice to delimit the data fields. Just think of an email
including a comma, be it one single line or not, wouldn't that be
interpreted as two fields, anyway?
/Second/, regarding multi-line emails, maybe { and } can be used:
{Customer} {server} {contact} {phone} {this is
a longer comment with
some
line-breaks.}
But, depending on the software it isn't too likely that you can have
different opening and closing separators as in { and }, so some other
approach may be needed.
For starters, so, have a look at the following IMPORT-TICKETS function:
------------------------------------------------------------------------
import-tickets: func [
{Imports tickets from raw data string.
Returns a block containing a bolean value indicating whether all raw
data has been parsed successfully
and a block of the tickets imported (may be empty and gets cleared with
every function call).}
raw [string!] "Raw string data to be imported"
/with "Delimiters to be used"
field-delimiter [char!] "delimiter between fields (defaults to %)"
ticket-delimiter [char!] "delimiter between fields (defaults to %,
too)"
/local tickets
] [
field-delimiter: any [field-delimiter #"%"]
ticket-delimiter: any [ticket-delimiter #"%"]
clear tickets: []
reduce [
parse/all raw [
some [
copy name to field-delimiter 1 skip
copy first-name to field-delimiter 1 skip
copy email to field-delimiter 1 skip
copy phone to field-delimiter 1 skip
[ copy comment to ticket-delimiter 1 skip
| copy comment to end
]
(insert/only tail tickets new-line/all reduce [trim
name trim first-name trim email trim phone trim comment] on)
]
]
new-line/all tickets on
]
]
------------------------------------------------------------------------
USAGE:
IMPORT-TICKETS raw /with field-delimiter ticket-delimiter
DESCRIPTION:
Imports tickets from raw data string.
Returns a block containing a bolean value indicating whether all raw
data has been parsed successfully
and a block of the tickets imported (may be empty and gets cleared with
every function call).
IMPORT-TICKETS is a function value.
ARGUMENTS:
raw -- Raw string data to be imported (Type: string)
REFINEMENTS:
/with -- Delimiters to be used
field-delimiter -- delimiter between fields (defaults to %)
(Type: char)
ticket-delimiter -- delimiter between fields (defaults to %,
too) (Type: char)
------------------------------------------------------------------------
probe import-tickets {Bill%Bilbo%bill-yahoo.com%555-555-5555%Please open
a ticket for this
server
in
line
4%
Bill%Bilbo%bill-yahoo.com%555-555-5555%Please open the ticket now!%
Random%Joe%joe-random.com%123-456-7890%Yet another
multi-line
comment}
------------------------------------------------------------------------
[true [
[
"Bill"
"Bilbo"
"bill-yahoo.com"
"555-555-5555"
{Please open a ticket for this
server
in
line
4}
]
[
"Bill"
"Bilbo"
"bill-yahoo.com"
"555-555-5555"
"Please open the ticket now!"
]
[
"Random"
"Joe"
"joe-random.com"
"123-456-7890"
"Yet another^/multi-line^/comment"
]
]]
------------------------------------------------------------------------
HTH,
Christian
John Blake schrieb: