Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2022-02

Putting Delimited text into a StringList

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I'm trying to get Comma Delimited Text from a CSV File created with Microsoft Excel into a string list, but I keep getting spaces as delimiter characters. How can I prevent this and use ONLY commas as delimiters? I hardcoded a test string in and it is delimiting on spaces and commas, but I wand only commas to be considered delimters.
I defined my delimiter character as , and used StrictDelmiter. How can I stop considering spaces as delimters?

Here is my code:
         CSVLineStringList:=TStringlist.Create;
         CSVLineStringList.Delimiter:=',';
         CSVLineStringList.StrictDelimiter;
         CSVLineStringList.DelimitedText:='This is a test,1234,5678';
         For I := 0 to CSVLineStringList.Count-1  do
            Begin
               Writeln(I+1,':' + CSVLineStringList[I]);
            End;
         CSVLineStringList.Free;

Here is my output:
1:This
2:is
3:a
4:test
5:1234
6:5678

I am wanting the output of this to be:
1: This is a test
2: 1234
3: 5678

James

Reply by email

Michael Van Canneyt
to James Richters
James Richters wrote 12 lines
I'm trying to get Comma Delimited Text from a CSV File created with Microsoft Excel into a string list, but I keep getting spaces as delimiter characters. How can I prevent this and use ONLY commas as delimiters? I hardcoded a test string in and it is delimiting on spaces and commas, but I wand only commas to be considered delimters. I defined my delimiter character as , and used StrictDelmiter. How can I stop considering spaces as delimters? Here is my code: CSVLineStringList:=TStringlist.Create; CSVLineStringList.Delimiter:=','; CSVLineStringList.StrictDelimiter;

This must be

CSVLineStringList.StrictDelimiter:=True;

But for the love of god, please stop using stringlist for CSV. it's really not meant for that.

Use the TCSVDocument or TCSVParser/TCSVBuilder.
They are much better at handling CSV data.

In the upcoming release they will be documented, but you can preview the documentation here:
https://www.freepascal.org/daily/doc/fcl/csvreadwrite/index.html https://www.freepascal.org/daily/doc/fcl/csvdocument/index.html

Michael.

Reply by email

James Richters
to Michael Van Canneyt

Thank you for correcting my error, that did work, but I think I will take your advice and try to use TCSVDocument, that looks like it will be much simpler and give me a 2 dimensional array which is what I really wanted to end up with.

Thanks for the documentation link and the suggestion!

James

Reply by email

James Richters
to James Richters

I'm trying to use TCSVDocument,

I have:

Uses Math,sysutils,Classes,CSVReadWrite;
That seems to be fine,
But I get an error 1D.pas(18,17) Error: Identifier not found "TCSVDocument"

On a line:

Var
   CSVData   :  TCSVDocument;

Do I need another unit or am I doing something wrong?

James

Reply by email

Tomas Hajny
to James Richters
James Richters wrote 16 lines
I'm trying to use TCSVDocument, I have: Uses Math,sysutils,Classes,CSVReadWrite; That seems to be fine, But I get an error 1D.pas(18,17) Error: Identifier not found "TCSVDocument" On a line: Var CSVData : TCSVDocument; Do I need another unit or am I doing something wrong?

CSVDocument?

Tomas

Reply by email