Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from a csv file

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I'm looking at creating a procedure that I use to read from a csv file. I am able to show the file contents in a Memo component using the syntax 'Memo1.Lines.LoadFromFile(..)'. Ideally I want a process to cycle through the lines in the indicated csv file and output a line into a string, then process this removing the commas such that I am able to feed the resultant strings into a database. Rather than re-create the wheel - I'm hoping someone who has already done this could give me a helping hand.
Thanks in advance
Steve
 
I don't know if this is what you're looking for but this is a function I use to parse a string into a TStringlist, with any delimiter


{ParseStringQual}
procedure ParseStringQual(str, delimiter, qualifier: string; tokens: TStringList);
var token: string;
x: Longint;
qualified: Boolean;
begin
tokens.Clear;
qualified := False;
for x := 1 to Length(str) do
begin
if (token='') and (str[x]=qualifier) and not(qualified) then
begin //we're at the beginning of a token, and it's qualified
qualified := True;
end
else if (str[x] = qualifier) and qualified then
begin //this is the ending qualifier, next delimiter signifies end
qualified := False;
end
else if (str[x] = delimiter) and not(qualified) then
begin //we're not between qualifiers and we found a delimiter
tokens.Add(Trim(token));
token := '';
end
else
begin
token := token + str[x];
end;
end;
tokens.Add(trim(token));
end;
TealWren
 
Hi Steven

I wrote this to read .csv files into a string grid.

function TMain.Loadfile(filename: Tfilename): boolean;
var F: TextFile;
S, TempS: string;
R,C: integer;
P: integer;
begin
AssignFile(F, filename);
reset(F);
Readln(F,S);
r := 0;
if pos(',',S) > 0 then
begin
StatusBar.Panels[2].text := 'READING CSV DATA';
sleep(500);
// comma read
c := 0;
reset(F);
while not eof(F) do
begin
Readln(F,S);
if pos(Filter,S) > 0 then
begin
inc(R);
Table.rowcount := R + 1;

for P := 1 to length(S) do
if copy(S,P,1) <> ',' then
TempS := tempS + copy(S,P,1)
else
begin
Table.Cells[C,R] := tempS;
TempS := '';
inc(c);
end;

if c = 5 then
begin
Table.Cells[C,R] := tempS;
TempS := '';
c := 0;
end;
end;
end;
Itemcount := R;
Table.repaint;
LoadFile := true;
StatusBar.Panels[2].text := 'DATA LOADED';

end
else
begin
showmessage('Data file format error. Not loaded');
StatusBar.Panels[2].text := 'NO DATA LOADED';
Loadfile := False;
end;
CloseFile(f);
with table do
if (atbottom.checked) and (rowcount > 10) then
TopRow := rowcount - ((Height div 20)-1);
StatusBar.Panels[0].text := DataFilename;
StatusBar.Panels[1].text := inttostr(itemcount);
end;


If it's any help

Steve..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top