Recent

Author Topic: [SOLVED] Do not use QuoteChar when save csv  (Read 577 times)

totya

  • Hero Member
  • *****
  • Posts: 719
[SOLVED] Do not use QuoteChar when save csv
« on: February 03, 2023, 10:04:34 pm »
Hi Master! :)

If I load and save same csv file, the result is different, see:

input:

Quote
a   b   c
öüó   őúé    áűí
123    456   789

output: (save)
Quote
a   b   c
öüó   "őúé "   áűí
"123 "   456   789

But I must do not need QuoteChar when I save csv file, can I do it anyhow? Thank you!

Code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MenuItem1Click(Sender: TObject);
  2. begin
  3.   CSVParams.Encoding:='UTF-8';
  4.   CSVParams.Delimiter := #9;
  5.  
  6. //CSVParams.QuoteChar :=  ???
  7.  
  8.   sWorksheetGrid1.LoadFromSpreadsheetFile('Src\test.csv', sfCSV);
  9.   sWorksheetGrid1.SaveToSpreadsheetFile('Src\test_OUT.csv', sfCSV);
  10. end;      
  11.  
« Last Edit: February 07, 2023, 08:11:45 pm by totya »

wp

  • Hero Member
  • *****
  • Posts: 10640
Re: Do not use QuoteChar when save csv
« Reply #1 on: February 07, 2023, 05:57:30 pm »
No "master", please...

It seems that quotes are added when a cell text ends (or begins) with a space. Looking into the csvdocument sources (which are used by fpspreadsheet) I see that there is a boolean option "QuoteOuterWhiteSpace" which is active by default. When you open file fpscsv.pas, find procedure TsCSVWriter.WriteSheet, add the line "FCSVBuilder.QuoteOuterWhiteSpace := CSVParams.QuoteOuterWhiteSpace;" after the "try", and add a "CSVParams.QuoteOuterWhiteSpace :=false;" to your project code the quotes will be gone.

I committed an updated version of fpspreadsheet in which the TCSVParams record is extended by the following fields:
Code: Pascal  [Select][+][-]
  1.   TsCSVParams = record   // W = writing, R = reading, RW = reading/writing
  2.     ...
  3.     QuoteOuterWhiteSpace: Boolean;   // W: Quote when cell content begins/ends with white space.
  4.     IgnoreOuterWhiteSpace: Boolean;  // R: Ignores white space before/after cell content.
  5.     ....
  6.   end;

As you can see you can also set CSVParams.IgnoreOuterWhiteSpace to true to get rid of the leading/trailing spaces already at reading - then these cells will not be quoted either.

But note that the white-space options do not work for numerical cells since the string-to-float conversion occurs via the standard function TryStrToFloat which does a "trim" (white-space removal) before the conversion.

Thaddy

  • Hero Member
  • *****
  • Posts: 12918
Re: Do not use QuoteChar when save csv
« Reply #2 on: February 07, 2023, 06:06:52 pm »
What is so difficult in understanding escaped chars?
https://en.wikipedia.org/wiki/Escape_character

To be honest @wp gave you a much more elaborate answer than the question deserved.
He has more patience...
« Last Edit: February 07, 2023, 06:13:23 pm by Thaddy »
In memory of Gordon Moore  (January 3, 1929 – March 24, 2023) Just double the heaven every two years from now.

totya

  • Hero Member
  • *****
  • Posts: 719
Re: Do not use QuoteChar when save csv
« Reply #3 on: February 07, 2023, 07:30:03 pm »
No "master", please...

Hi!

I call you "master" long time ago (years), because this is the biggest component what I seen ever on the world, and this component helped me a lot. But okay!

Your completely understand my problem (not like others) thank you!

I uninstall fps comoonent via online package manager. First uninstall nothing happen, but the second uninstall is success. Next I install svn version, success.

Unfortunately,  CSVParams.QuoteOuterWhiteSpace := False; doesn't work.

Code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadCSVMenuItemClick(Sender: TObject);
  2. begin
  3.   CSVParams.Encoding := 'UTF8';
  4.   CSVParams.Delimiter := #9;
  5.   CSVParams.QuoteOuterWhiteSpace := False;
  6.  
  7.   Memo.Lines.Add('Loading...');
  8.   Application.ProcessMessages;
  9.  
  10.   sWorksheetGrid1.LoadFromSpreadsheetFile('Src\test.csv', sfCSV);
  11.   sWorksheetGrid1.SaveToSpreadsheetFile('Src\test_OUT.csv', sfCSV);
  12. end;        
  13.  

input:
Quote
a   b   c
öüó   őúé    áűí
123    456   789

output:
Quote
a   b   c
öüó   "őúé "   áűí
"123 "   456   789

wp

  • Hero Member
  • *****
  • Posts: 10640
Re: Do not use QuoteChar when save csv
« Reply #4 on: February 07, 2023, 08:04:31 pm »
Oh, there's a typo in the writer code - fixed. Please get yourself the latest commit.

totya

  • Hero Member
  • *****
  • Posts: 719
Re: Do not use QuoteChar when save csv
« Reply #5 on: February 07, 2023, 08:11:08 pm »
Oh, there's a typo in the writer code - fixed. Please get yourself the latest commit.

Perfect, works, thank you wp!

 

TinyPortal © 2005-2018