Recent

Author Topic: Read one line of CSV file using TStringList with delimiter  (Read 186 times)

thompsna

  • New member
  • *
  • Posts: 7
Read one line of CSV file using TStringList with delimiter
« on: September 12, 2019, 10:27:41 pm »
Hi I'm looking to read in a large CSV file one line at a time so I can pull out certain columns.  For example, one row might be:

AAA,111,BBB,222,CCC,333

...and I want to pull the third column to get a string of "BBB" the put it in an array called SourceID.  The post at https://forum.lazarus.freepascal.org/index.php?topic=42696.0 was a huge help, but the example at the bottom is single characters, and when I apply it I am only getting single characters too.


Code: Pascal  [Select]
  1.       readln(SourceFile, InputLine);
  2.       NumberSourceRows := NumberSourceRows +1;
  3.                                                      
  4.       SourceStringList.delimiter := char(','); //delimiter used
  5.       SourceStringList.text := InputLine; //fill text
  6.       SourceStringList.DelimitedText := SourceStringList.Text;
  7.  
  8.       SourceID[NumberSourceRows] := SourceStringList.DelimitedText[3];
  9.  

This gets me the third character which would be A, not the third delimited string.  I want the third delimited string.

Any help is appreciated!

krexon

  • Jr. Member
  • **
  • Posts: 67
Re: Read one line of CSV file using TStringList with delimiter
« Reply #1 on: September 12, 2019, 10:37:47 pm »
@thompsna
Code: Pascal  [Select]
  1. SourceStringList[2]

bytebites

  • Full Member
  • ***
  • Posts: 206
Re: Read one line of CSV file using TStringList with delimiter
« Reply #2 on: September 12, 2019, 10:41:11 pm »
Code: Pascal  [Select]
  1. var
  2.  s:string;
  3. begin
  4.   s:='AAA,111,BBB,222,CCC,333';
  5.   writeln(s.Split([','])[2]);
  6. end;
  7.  

howardpc

  • Hero Member
  • *****
  • Posts: 3155
Re: Read one line of CSV file using TStringList with delimiter
« Reply #3 on: September 12, 2019, 11:05:48 pm »
Some ideas for you here:
Code: Pascal  [Select]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.    Classes, Types, SysUtils;
  7.  
  8. procedure FillArray(out anArray: TStringDynArray; const aCSVFilename: String; aColumn: Byte);
  9. var
  10.   sl: TStringList;
  11.   csvF: TextFile;
  12.   s: String;
  13. begin
  14.   if not FileExists(aCSVFilename) then
  15.     begin
  16.       WriteLn('Cannot find ',aCSVFilename);
  17.       Exit;
  18.     end;
  19.   SetLength(anArray, 0);
  20.   sl := TStringList.Create;
  21.   try
  22.     sl.Delimiter := ',';
  23.     AssignFile(csvF, aCSVFilename);
  24.     Reset(csvF);
  25.     try
  26.       while not EOF(csvF) do
  27.         begin
  28.           ReadLn(csvF, s);
  29.           WriteLn(s);
  30.           sl.CommaText := s;
  31.           if aColumn < sl.Count then
  32.           begin
  33.             SetLength(anArray, Length(anArray)+1);
  34.             anArray[High(anArray)] := sl[aColumn-1];
  35.           end;
  36.         end;
  37.     finally
  38.       CloseFile(csvF);
  39.     end;
  40.   finally
  41.     sl.Free;
  42.   end;
  43. end;
  44.  
  45. var
  46.   arr: TStringDynArray = Nil;
  47.   s: String;
  48. begin
  49.   FillArray(arr, 'text.csv', 3);
  50.   for s in arr do
  51.     WriteLn(s);
  52.   ReadLn;
  53. end.

thompsna

  • New member
  • *
  • Posts: 7
Re: Read one line of CSV file using TStringList with delimiter
« Reply #4 on: September 12, 2019, 11:17:59 pm »
Awesome, thank you @krexon, that's all I needed!  Thank you to the rest for replying too!