delphi spirit, forever

09 Nov, 2008

How do you parser CSV file use delphi

Posted by: admin In: delphi tips

The wpsCsvParser has been updated to support parsing Tab Separated Value files. You can specify the value separator type by setting the new ValueSeparatorType property to either vsCsv or vsTab. Or you can set the ValueSeparator property to any character you wish to use as the separator. The default is vsCsv for CSV parsing. From here.

I have used it in one of my program and feel good.

Sample code like this:

procedure CsvParserSample;
const
  CsvFileName = 'sample01.csv';
var
  parser: TwpsCsvParser;
  lineCount: integer;
begin
  lineCount := 0;
  parser := TwpsCsvParser.Create;
  try
    parser.LoadFromFile(CsvFileName);
    while parser.ReadLn do
    begin
      Writeln(parser.Fields[0]);
      Inc(lineCount);
    end;
  finally
    parser.Close;
  end;
end;

Related posts

Tags:

2 Responses to "How do you parser CSV file use delphi"

1 | Gustav

August 24th, 2009 at 7:38 pm

Avatar

Good component, i use it , to count the number of cols i use it:, is trivial but i waste 2hours trying know why the number of fields was 0.

while parser.ReadLn do
begin
//do something more
var:=parser.Fields.Count;
end;

//we assume that there is the same number of cols.
//if we use parser.Fields.Count at the end of the while the value is 0;

2 | crystalbit

January 14th, 2010 at 1:44 am

Avatar

Thanks a lot, a very helpful component. But I prefer winapi, because it works faster.
And hello from Russia :)

Comment Form