Re: Parsing a CSV file
From: Anders Isaksson (anders.isaksson_at_REMOVEcej.se)
Date: 12/03/03
- Next message: Anders Isaksson: "Re: Rounding issue in TBCDField"
- Previous message: Andrea Raimondi: "Re: Inherited and D5 help file"
- In reply to: Al Vas: "Parsing a CSV file"
- Next in thread: Perry Way: "Re: Parsing a CSV file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 03 Dec 2003 08:39:45 +0100
On Tue, 2 Dec 2003 20:17:14 +1100, "Al Vas" <alex@au.com.favour> wrote:
>Des anyone have any code or know where I can find code that shows how to
>parse a CSV file into separate values. I have tried but it all seems very
>complicated, especially since some values (such as dates dont need quotes
>around them). An examples that highlights the difficulty is the following
>txt line:
>
>01/01/2003,"My Name, Your Name",03/03/2003,114.2,"My
>Address",95,01/01/2003,03/03/2003,
>
>10 fields are represented in this example. I have tried counting
>doublequotes and separators but cant get it working satisfactorily.
Write a little 'State Machine' for scanning the string, replacing the
delimiters with #$0D (RETURN) on the way. Then assign the result to a
TStringList.Text (the StringList must be created by the caller).
(uncompiled code)
type
TScanState = (ssNormal, ssInQuotes);
procedure ParseCSV(const Line: string; Delim, QuoteChar: Char; Result:
TStringList);
var
i: integer;
tmpStr: string;
State: TScanState;
begin
State := ssNormal;
tmpStr := Line;
for i := 1 to Length(tmpStr) do begin
case State of
ssNormal:
if tmpStr[i] = Delim then begin
tmpStr[i] := #$0D;
end
else if tmpStr[i] = QuoteChar then begin
State := ssInQuotes;
end;
ssInQuotes:
if tmpStr[i] = QuoteChar then begin
State := ssNormal;
end;
end; // case
end; // for
if State <> ssNormal then // Some error in the Line
Result.Text := tmpStr;
end;
Not handled by this routine:
- If you have both "-quotes and '-quotes in the same line.
- Quotes within quotes: 'This is Anders'' computer'.
-- Anders Isaksson, Sweden BlockCAD: http://user.tninet.se/~hbh828t/proglego.htm Gallery: http://user.tninet.se/~hbh828t/gallery/index.htm
- Next message: Anders Isaksson: "Re: Rounding issue in TBCDField"
- Previous message: Andrea Raimondi: "Re: Inherited and D5 help file"
- In reply to: Al Vas: "Parsing a CSV file"
- Next in thread: Perry Way: "Re: Parsing a CSV file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|