Forum > Databases

Tools for manage CSV files

<< < (3/3)

mas steindorff:

--- Quote from: Zvoni on August 31, 2022, 12:17:36 pm ---A StringList is IMO overkill for a simple string delimited with commas.
Just split it.

--- End quote ---
IMO Stringlists are a good object to learn and are a simple fix for this issue.
I find I use them in most of my programs for one thing or another.  They are also integrated into a lot of components already so (like a memo) so you should not avoid them too long.
MAS

Zvoni:

--- Quote from: mas steindorff on August 31, 2022, 09:48:42 pm ---
--- Quote from: Zvoni on August 31, 2022, 12:17:36 pm ---A StringList is IMO overkill for a simple string delimited with commas.
Just split it.

--- End quote ---
IMO Stringlists are a good object to learn and are a simple fix for this issue.
I find I use them in most of my programs for one thing or another.  They are also integrated into a lot of components already so (like a memo) so you should not avoid them too long.
MAS

--- End quote ---
Provided OP is actually coding an app with GUI (since you mentioned a Memo)

Bottom line: not enough information about OP‘s problem

egsuh:

--- Quote ---Egsuh thanks for the answer, I think the solution is to join the CSV files in one, my main problem is that the CSV files are coming to me in rows, so it does not meet the structure of the DB, in order to insert the data correctly I must convert the CSV data to columns.

data1,data2,data3...

convert to.

data1
data2
data3
.
.

Thank you very much for your attention.
--- End quote ---

Do the coversion in Excel manually. If your file is small,

          1.  Open it in Excel. Then there would be a long row. 
          2.  Copy the row, and paste it with column/row converted in a new sheet. 

This is the simplelist. Just a few mouse clicks.
Once all four files are done, copy the columns into one Sheet and save it as a new CSV file.


If your files are very large, I suggested to use read statement, similar to FORTRAN method.

              read (f1, aninteger)
 
will read in only one integer number (not the whole line), if your data are delimited with blank (not sure about comma).  So,


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---        while not eol(f1) do begin             read (f1, aninteger);             writeln(f2, aninteger);         end; will convert row to column.

If your files are medium sized, using TStringList may be better.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---             readln (inf1, s); StringList1.CommaText := s;                readln(inf2, s); StringList2.Commatext := s;              readln(inf3, s); StringList3.Commatext := s;              readln(inf4, s); StringList4.Commatext := s;              for ti:= 0 to AStringList.Count-1 do                   writeln(outf, StringList1[ti], ',', StringList2[ti],',',                                   StringList3[ti],',',  StringList4[ti]); You may make a new file in this way.

egsuh:
Hi, I'm sorry that I did not notice that "read" statement cannot read TDate type directly from string.
I made up a sample project. Of course there are way-arounds.
Displaying in memo1 is just to visually check.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var   f : TextFile;   i: integer;   s: string;    DateList,   TimeList,   DataList,   IngList : TStringList; begin   DateList := TStringList.Create;   TimeList := TStringList.Create;   DataList := TStringList.Create;   IngList := TStringList.Create;    try      AssignFile(f, 'date.txt');      Reset(f);      readln(f, s);      DateList.Commatext := s;      CloseFile(f);       AssignFile(f, 'time.txt');      Reset(f);      readln(f, s);      TimeList.Commatext := s;      CloseFile(f);       AssignFile(f, 'data.txt');      Reset(f);      readln(f, s);      DataList.Commatext := s;      CloseFile(f);       AssignFile(f, 'ingredient.txt');      Reset(f);      readln(f, s);      IngList.Commatext := s;      CloseFile(f);       AssignFile (f, 'AllData.csv');      Rewrite(f);       for i:= 0 to DateList.Count-1 do begin         s:= Format('%d, %s, %s, %s, %s',                   [i+1, DateList[i], TimeList[i], DataList[i], IngList[i]]);         memo1.lines.Add(s);  // This is just to show. Not necessary.         writeln(f, s);      end;      Closefile(f);   finally      DateList.Free;      TimeList.Free;      DataList.Free;      IngList.Free;   end;end;

440bx:
If the objective is just to put each comma delimited string into its own line, sounds like the Lazarus text editor could do it in a single command "replace comma, crlf"

save the file (possibly with a new name)... done.


Navigation

[0] Message Index

[*] Previous page

Go to full version