 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ben Crain Guest
|
Posted: Thu Oct 26, 2006 8:52 pm Post subject: Import numerical data |
|
|
I need a component -- or maybe just a procedure or some code -- that will
import numerical data from a text file. It must be from an ordinary text
file -- not a data base -- that has data arranged in columns. So it must
recognize how many columns there are, how many data values per column,
distinguish numerical data from whatever else might be in the text file and
ignore the other stuff, if it's not in a column, or import it as missing
data (possibly NAN) if it is. Or import nothing, and display an appropriate
message, if there are no data columns.
Thanks for suggestions...
Ben Crain |
|
| Back to top |
|
 |
Larry Maturo Guest
|
Posted: Thu Oct 26, 2006 9:29 pm Post subject: Re: Import numerical data |
|
|
Hi Ben,
Do you know before hand how many columns there are, at which
character position each one starts, their length, and data type? If
so you can use a config file to define the columns, and parse the data
out. If not, you can use the first row as a template to determine what
each column type is, and where they start, and then use that to parse
the rest of the file.
Off hand I don't know of any components or readily available code
that will do this. Sorry.
-- Larry Maturo
"Ben Crain" <bcrain (AT) verizon (DOT) net> wrote in message
news:4540d9b3 (AT) newsgroups (DOT) borland.com...
| Quote: | I need a component -- or maybe just a procedure or some code -- that will
import numerical data from a text file. It must be from an ordinary text
file -- not a data base -- that has data arranged in columns. So it must
recognize how many columns there are, how many data values per column,
distinguish numerical data from whatever else might be in the text file and
ignore the other stuff, if it's not in a column, or import it as missing
data (possibly NAN) if it is. Or import nothing, and display an
appropriate message, if there are no data columns.
Thanks for suggestions...
Ben Crain
|
|
|
| Back to top |
|
 |
Guest
|
Posted: Thu Oct 26, 2006 10:25 pm Post subject: Re: Import numerical data |
|
|
Hi, Ben:
You might have a look at the SMImport Suite over at www.scalbium.com.
This product might be overkill for what you're trying to do, but it
will certainly do the job. I'm a happy user.
Greg
Ben Crain wrote:
| Quote: | I need a component -- or maybe just a procedure or some code -- that will
import numerical data from a text file. It must be from an ordinary text
file -- not a data base -- that has data arranged in columns. So it must
recognize how many columns there are, how many data values per column,
distinguish numerical data from whatever else might be in the text file and
ignore the other stuff, if it's not in a column, or import it as missing
data (possibly NAN) if it is. Or import nothing, and display an appropriate
message, if there are no data columns.
Thanks for suggestions...
Ben Crain |
|
|
| Back to top |
|
 |
Ben Crain Guest
|
Posted: Thu Oct 26, 2006 10:36 pm Post subject: Re: Import numerical data |
|
|
<gvbishop (AT) gmail (DOT) com> wrote in message
news:1161883507.438834.4080 (AT) f16g2000cwb (DOT) googlegroups.com...
| Quote: | Hi, Ben:
You might have a look at the SMImport Suite over at www.scalbium.com.
This product might be overkill for what you're trying to do, but it
will certainly do the job. I'm a happy user.
Greg
|
Thanks, I'll give it a look-see. |
|
| Back to top |
|
 |
Caleb Hattingh Guest
|
Posted: Fri Oct 27, 2006 8:12 am Post subject: Re: Import numerical data |
|
|
I needed the same functionality. I hacked this together recently:
DataList := TStringsObjectList.Create;
try
if not TryLoadColumnarFileIntoListOfTStrings(DataList,
FEEDSFILE, ',', '#', MessagesStrings) then
exit;
// Do stuff with datalist
finally
DataList.Free;
end;
where
TStringsObjectList = class(TObjectList)
protected
procedure SetStrings(i: integer; ts: TStrings);
function GetStrings(i: integer): TStrings;
public
property Items[i: integer]: TStrings read GetStrings write
SetStrings; default;
function Add(ts: TStrings): integer;
constructor Create;
end;
with method definitions
function TStringsObjectList.Add(ts: TStrings): integer;
begin
result := inherited Add(ts);
end;
constructor TStringsObjectList.Create;
begin
Self.OwnsObjects := True;
end;
function TStringsObjectList.GetStrings(i: integer): TStrings;
begin
result := inherited Items[i] as TStrings;
end;
procedure TStringsObjectList.SetStrings(i: integer; ts: TStrings);
begin
inherited Items[i] := ts; // Does memory of the old one get freed
here?
end;
and
function TryLoadColumnarFileIntoListOfTStrings(var tsol:
TStringsObjectList;
filename: string; Delimiter: char = ','; CommentSymbol: string = '';
messages:
TStrings = nil): boolean;
var
i, j, columns: integer;
ts1, ts2: TStringList;
begin
result := False;
if not Assigned(tsol) then
begin
messages.Add('"tsol" argument to'
+ ' function TryLoadColumnarFileIntoListOfTStrings(tsol:
TStringsObjectList;'
+ ' filename: string; messages: TStrings = nil): boolean;);'
+ ' was not created yet!.');
exit;
end;
// Detecting columns
ts1 := TStringList.Create;
ts2 := TStringList.Create;
try
if not TryLoadFileIntoStrings(ts1, filename, messages,
CommentSymbol) then
exit;
if ts1.Count = 0 then
begin
messages.Add('File was empty');
exit;
end;
ts2.Delimiter := Delimiter;
ts2.DelimitedText := ts1[0];
columns := ts2.Count;
for i := 0 to columns - 1 do
begin
tsol.Add(TStringList.Create); // The container will manage this
memory
end;
for i := 0 to ts1.Count - 1 do
begin
ts2.Clear;
ts2.DelimitedText := ts1[i];
if ts2.Count <> columns then
begin
messages.Add(Format('Row %d in file: %s doesn''t have the same
number of '
+ 'columns as the first row.', [i, filename]));
exit;
end;
for j := 0 to columns - 1 do
tsol[j].Add(ts2[j]);
end;
result := true;
finally
ts1.Free;
ts2.Free;
end;
end;
It doesn't handle weird situations like non-numeric data mixed up in
unpredictable ways, but will put each column of data in the input file
into its own StringList, which is what I wanted.
Thanks
Caleb
Ben Crain wrote:
| Quote: | I need a component -- or maybe just a procedure or some code -- that will
import numerical data from a text file. It must be from an ordinary text
file -- not a data base -- that has data arranged in columns. So it must
recognize how many columns there are, how many data values per column,
distinguish numerical data from whatever else might be in the text file and
ignore the other stuff, if it's not in a column, or import it as missing
data (possibly NAN) if it is. Or import nothing, and display an appropriate
message, if there are no data columns.
Thanks for suggestions...
Ben Crain |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|