Re: Parsing a CSV file

From: Anders Isaksson (anders.isaksson_at_REMOVEcej.se)
Date: 12/03/03


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


Relevant Pages

  • Re: understanding arrays, and their use
    ... The fourth field can be a string enclosed in quotes with either a file path, or multiple words separated by spaces. ... Now, separate a line into four groups, with whitespace separating them. ... set filehandle ...
    (comp.lang.tcl)
  • Re: Execute command line switches from vbs
    ... Command Line For Access Macro ... My goal is to reduce numerous separate scheduled tasks to only several VBS ... VBS literal string. ... doubling up the quotes that are to be part of intended ...
    (microsoft.public.scripting.vbscript)
  • Re: Letter AND Merge by Category
    ... I played around with the instructions you sent me, but when I try to merge, ... The constant in the mail merge is the Sales Manager name. ... I am not putting quotes in the proper places. ... separate page after the body of the letter. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: how to explain the expression in single quotes?
    ... >> The shell strips quotes from the line. ... >> Because echo prints its arguments separated by spaces. ... >> contains on the command line as a separate argument. ...
    (comp.unix.shell)
  • Re: Moving text
    ... What version Publisher are you using? ... Was your manuscript ... Have you considered putting the long quotes in a ... separate text box or a Design Gallery sidebar? ...
    (microsoft.public.publisher)