Recent

Author Topic: Program that reads CSV and corrects it  (Read 1715 times)

nerdynick

  • Newbie
  • Posts: 2
Program that reads CSV and corrects it
« on: March 14, 2016, 01:03:45 pm »
Hi there,

I'm new and this is my first post. I've created a program in Lazarus that imports and reads 2 CSV files into DBGrids and compares the content of CSV2 to CSV1 on a row-by-row basis and if the content of CSV1 is different, it changes it to that of CSV2. It is a very simple exercise for my school - to make it easier for IT teachers to rectify pupils' mistakes when they fill in their data, plus I'm just testing out the DBGrids in Lazarus and seeing how it works with the datasources and datasets.

Everything is fine, apart from now I want to go a step further and make my program able to compare the CSV1 file to CSV2 from a given row, e.g. not right from the start, but from a certain date that is contained in both of these files.

Here is my code that compares and corrects on a row-by-row and column-by-column basis:

Code: Pascal  [Select][+][-]
  1. procedure TForm2.Button4Click(Sender: TObject);
  2. var
  3.   i:integer;
  4. begin
  5.    sdfdataset1.insert;
  6.    //sdfdataset2.active:=true;
  7.    //i:=0;
  8.    with sdfdataset1 do
  9.    begin
  10.      //DisableControls;
  11.      First;
  12.   //SdfDataSet1.DisableControls;
  13.     try
  14.       SdfDataSet1.First();
  15.       SdfDataSet2.First();
  16.       while (not Sdfdataset1.Eof) do
  17.       begin
  18.        //Edit;
  19.        for i:= 0 to 3 do
  20.          begin
  21.           Edit;
  22.          if Sdfdataset1.Fields[i].Text <> Sdfdataset2.Fields[i].Text then
  23.             Sdfdataset1.Fields[i].Text:= Sdfdataset2.Fields[i].Text;
  24.             //Sdfdataset1.Post;
  25.  
  26.          end;
  27.        with sdfdataset2 do
  28.        next;
  29.        //i:=i+1;
  30.        Next;
  31.       end;
  32.  
  33.     finally
  34.     Sdfdataset1.EnableControls;
  35.     ShowMessage('Save file as ' + edit3.text);
  36.  
  37.  
  38.     end;

P.S. I'm not sure how to export a loaded CSV file from a program. I would be grateful if someone could point me in the right direction.

Thanks again,
nerdynick

nerdynick

  • Newbie
  • Posts: 2
Re: Program that reads CSV and corrects it
« Reply #1 on: March 14, 2016, 02:05:54 pm »
I found something that might help me - a simple way to get user-selected row and column...

Still stuck, though....

mig-31

  • Sr. Member
  • ****
  • Posts: 286
Re: Program that reads CSV and corrects it
« Reply #2 on: March 14, 2016, 02:50:43 pm »
Hi,

if you don't want to make a file copy.

By your explanation you compare CVS1 to CVS2. You should checkfirst if CVS2 not EOF, then check if CVS1 not EOF
Code: Pascal  [Select][+][-]
  1.     while NOT(SdfDataSet2.EOF) do begin
  2.       if SdfDataSet1.EOF then  begin
  3.         SdfDataSet1.Insert;
  4.         for i:=0 to SdfDataSet2.FieldCount-1 do SdfDataSet1.Fields.Fields[i].Text:=SdfDataSet2.Fields.Fields[i].Text;
  5.         SdfDataSet1.Post;
  6.       end else
  7.         for i:=0 to SdfDataSet2.FieldCount-1 do begin
  8.           if SdfDataSet1.Fields.Fields[i].Text <> SdfDataSet2.Fields.Fields[i].Text then
  9.             SdfDataSet1.Fields.Fields[i].Text := SdfDataSet2.Fields.Fields[i].Text;
  10.             SdfDataSet1.Next;
  11.        end;
  12.       SdfDataSet2.Next;
  13.     end;
  14.    //check if CVS1 don't have more rows
  15.     while NOT(SdfDataset1) then begin
  16.       SdfDataset1.Delete;
  17.       SdfDataset1.Post;
  18.     end;    
  19.     SdfDataSet1.SaveToFile(FileName);
  20.  

Additionally you should check
1) if CVS2 exists and not empthy,
2) if collumn count in CVS1 is the same as CVS2.
Lazarus 2.0.6 - CentOS 7.x, Mageia 7.1

 

TinyPortal © 2005-2018