Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parsing a CSV file with delphi

1,226 views
Skip to first unread message

matthew

unread,
Jan 4, 2005, 3:53:57 PM1/4/05
to
I am wondering how to parse a csv file with delphi?

Matt

Mike Shkolnik

unread,
Jan 4, 2005, 4:22:17 PM1/4/05
to
SMImport suite have a component for extended CSV parsing and loading to
database:
http://www.scalabium.com/smi

--
With best regards, Mike Shkolnik
E-mail: mshk...@scalabium.com
WEB: http://www.scalabium.com

"matthew" <n...@email.com> wrote in message
news:41db028f$1...@newsgroups.borland.com...

Liz

unread,
Jan 4, 2005, 4:00:10 PM1/4/05
to
matthew wrote:

> I am wondering how to parse a csv file with delphi?

You can load csv files into a number of components by default I
believe, or parsee through it yourself with pos and copy :)

Robert

unread,
Jan 4, 2005, 6:12:56 PM1/4/05
to

"matthew" <n...@email.com> wrote in message
news:41db028f$1...@newsgroups.borland.com...
> I am wondering how to parse a csv file with delphi?
>

Assuming a tab delimited file.

var f : textfile;
l : tstringlist;
s : string;
...
assignfile(f, 'myfile.txt');
reset(f);
l := tstringlist.create;
l.delimiter := #9;
while not eof(f) do begin
readln(f, s);
// the instruction below needed only if you have embedded blanks
s := StringReplace(s, ' ', ' _', [ftReplaceAll]);
l.DelimitedText := s;

at this point, the tstringlist contains the fields in your original string.
You will have to replace the _ back to spaces on each line, if needed. This
bit is required because delimitedtext will break not only on tab but also
when finding a space.

Robert

Ian Kirk

unread,
Jan 4, 2005, 6:05:30 PM1/4/05
to
On Tue, 04 Jan 2005 14:53:57 -0600, matthew wrote:

> I am wondering how to parse a csv file with delphi?

Assuming its not too huge you can load a file into a TStringList using the
LoadFromFiles method. You can then use a second TStringList to parse each
line into individual items - just assign the second list's CommaText
property from one of the lines in the first list.

If more parsing than this is needed let us know what you're trying to do!

HTH,
--
Ian

0 new messages