* * *

Author Topic: Loading Only Certain Columns of a CSV File  (Read 431 times)

Jade

  • Newbie
  • Posts: 4
Loading Only Certain Columns of a CSV File
« on: August 17, 2017, 04:51:10 pm »
I am New to Lazarus and I have figured out how to load my CSV file with the SdfDataSet but I can not figure out how to display only certian parts of the CSV file. I only want to read in Col1, Col3, and Col34 of my file into my Grid. I have tried with a DataBaseGrid and a StringGrid. The DataGrid I have gotten to read just the first column but the other ones i can not figure out. Any help would be appreciated.

Almir.Bispo

  • Jr. Member
  • **
  • Posts: 57
  • CSV Comp DB is the Best NoSQL
    • CSV Comp DB (NoSQL)
Re: Loading Only Certain Columns of a CSV File
« Reply #1 on: August 17, 2017, 07:15:22 pm »
Hi ! I'm Lazarus and csv expert.
You can do like this:
Code: Pascal  [Select]
  1. procedure load(csvf,csvd:string);
  2. var s,d,res:tstringlist;
  3.        i:integer;
  4. begin
  5. s:=tstringlist.create;
  6. d:=tstringlist.create;
  7. res:=tstringlist.create;
  8. d.delimiter:=char(';'); //define delimiter used on csv
  9. s.loadfromfile(csvf);//file (csv table) loaded
  10. for i:= 0 to s.count-1 do
  11. begin
  12. d.delimitedtext:=s.strings[i]; //each row of file (table)
  13. res.add(d.strings[0]);//define wich column by index number "zero" is the first column
  14. end;
  15. res.savetofile(csvd);
  16.  
  17. res.free;
  18. s.free;
  19. d.free;
  20.  
  21. end;
  22. //how to use
  23. //load('c:\origenal.csv','c:\destin.csv');
  24. //sdf.active:=false;
  25. //sdf.filename:='c:\destin.csv';
  26. //sdf.active:=true;
  27.  

But if do you want to work with csv like a true Database System,I guess you know the NoSQL CSV Comp DB.
The solution is like this:
Code: Pascal  [Select]
  1. var CQL:Stringlist;
  2.       Column:integer;
  3. begin
  4. CQL:=Tstringlist.create;
  5. //what column to list?
  6. Column:= 0;//the first colmun
  7. //CQL Command
  8. CQL.add('{csv_table;@listar;0;0;0;0;query='+ inttostr(Column) +';destino=0}');
  9.  
  10. //save to imput os database system
  11. CQL.savetofile(application.location+'inpout_comp.exe');
  12. //execute the csv parser database system
  13. shellexecute(handle,pchar('open'),pchar(application.location+'CSV_PARSER.exe'),nil,nil,0);
  14. //wait execution
  15. //it depend on SO latency
  16. while not fileexiste('lock.inf') do begin {do nothing} end;
  17. while  fileexiste('lock.inf') do begin {do nothing} end;
  18. SDF.active:=false;
  19. SDF.filename:='csv_table.que'; //result table
  20. SDF.Active:=true;
  21. end;
  22.  
//The cql code is like:
Code: Javascript  [Select]
  1. {
  2.     path\csv_table;
  3.     @listar;
  4.     0;
  5.     0;
  6.     0;
  7.     0;
  8.     query=0;//define the Column here
  9.     destino=0
  10. }
  11.  
There are exemple with Lazarus and NoSQL http://adltecnologia.blogspot.com.br
CSV Comp DB Developer {Pascal Lover}

Jade

  • Newbie
  • Posts: 4
Re: Loading Only Certain Columns of a CSV File
« Reply #2 on: September 06, 2017, 06:24:23 pm »
thank you I actually finally got it to read i had the delimiter set to ; and my CSV file was separated by ,.
But any help on my new question i just posted would be appreciated.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus