Recent

Author Topic: [SOLVED] Load CSV to StringGrid  (Read 9617 times)

Sheepdog

  • New Member
  • *
  • Posts: 41
[SOLVED] Load CSV to StringGrid
« on: September 30, 2016, 10:56:15 pm »
How hard can it be, I wondered, to load some data from a textfile to a StringGrid. Such an obvious want... surely it's easy enough?

Well, two days later, I have come out the other side.

How To, with sourcecode, at...

http://sheepdogguides.com/lut/lt4Nd.htm

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Load CSV to StringGrid
« Reply #1 on: September 30, 2016, 11:08:33 pm »
Are you aware that the Lazarus TStringGrid has public LoadFromCSVFile and LoadFromCSVStream methods, with corresponding SaveToCSV... methods?

Sheepdog

  • New Member
  • *
  • Posts: 41
Re: [SOLVED] Load CSV to StringGrid
« Reply #2 on: September 30, 2016, 11:13:55 pm »
Re- "Are you aware..."

Thank you... but...

1) I want my file loaded CORRECTLY!  :)
(When I tried LoadFromCSVFile just now, spaces as well as commas were treated as field delimiters!)

2) I read about those along the way... and also read that I needed to install "extra" stuff on top of my "basic" Lazarus IDE to use them. (This was at http://wiki.freepascal.org/CsvDocument)

My solution works without the "extras"...

ALthough investigations just now suggest that maybe what I read was out of date. My solution leaves me "in the driver's seat" in respect of details, anyway. For instance, I can choose whether (or not) to trim spaces from either end of data as it loads. Perhaps not entirely an answer to "why do it the hard way?", I admit.

I wondered: Does the "built in" LoadFromCSVFile automatically "size" the StringGrid to suit the data in the file being loaded? My newbie experiments suggest that it does... but the number of columns is(?) entirely controlled by the number of fields in the first line... easily added to my "answer". My version already creates rows as needed, as does LoadFromCSVFile.

(For others with Google skills as limited as mine, the place I SHOULD have gone, when I was looking to see if wheel already invented was...

http://wiki.freepascal.org/Grids_Reference_Page
« Last Edit: September 30, 2016, 11:44:05 pm by Sheepdog »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: [SOLVED] Load CSV to StringGrid
« Reply #3 on: October 01, 2016, 05:03:03 am »
1) I want my file loaded CORRECTLY!  :)
(When I tried LoadFromCSVFile just now, spaces as well as commas were treated as field delimiters!)

If true, it sounds like a bug. Care to share a small project that shows the problem?

Thaddy

  • Hero Member
  • *****
  • Posts: 16043
  • Censorship about opinions does not belong here.
Re: [SOLVED] Load CSV to StringGrid
« Reply #4 on: October 01, 2016, 07:14:44 am »
1) I want my file loaded CORRECTLY!  :)
(When I tried LoadFromCSVFile just now, spaces as well as commas were treated as field delimiters!)

If true, it sounds like a bug. Care to share a small project that shows the problem?

Set StrictDelimiter to true, if it has that property. Otherwise load into a stringlist first and do the same.
[edit]
Actually it is not a bug, but an omission. The LoadFromCSV doesn't have the option StrictDelimiter and uses different code from TStringlist's CSV parsing. (Why? Wheels are round, not hexagonal anymore)
A space is always a valid separator unless in quoted parts and unless Strict is true.
I ended up simply using a TStringlist with StrictDelimiter set to true and fill the stringgrid by iterating over the stringlist, which obtained the results one expect.

Should be easily turned into a patch for TStringGrid when needed.
« Last Edit: October 01, 2016, 09:16:52 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018