|
Previous Top Next |
Prototype: | (STRING pLine, Byte pStringsAreQuoted=TRUE,<String pDelimiter>, <STRING pQuoteChar>),LONG |
pLine | The line to parse. It should contain valid CSV data, either comma separated or semicolon separated. |
pStringsAreQuoted | Indicates if the strings are quoted with double quotes. Defaults to TRUE. |
pDelimiter | Optionally set the delimiter to use (single character). This is the character that separate the fields in the CSV line. If it is not specified, comma (,) is used. |
pQuoteChar | Optionally set the character used to quote strings. If not specified double quotes (") is used. Added 2015-09-12 |
Returns | Number of fields parsed out of the line. |
This method takes a string and parses out comma separated (CSV) field data from it. Normally this data is formatted with commas separating the fields, strings inside double quotes and numbers unquoted. The method takes into account if the delimiter is inside quoted strings and will ignore them. This will only work properly in well formatted CSV data string.
Example:
Parsing a normal comma separated file:
ITS ITStringClass
S STRING(255)
I LONG
F LONG
X LONG
CODE
ITS.FileToLines('testcsv.csv')
LOOP I = 1 TO Records(ITS.Lines)
GET(ITS.Lines,I)
F = ITS.ParseCSVLine(ITS.Lines.OL)
ITS.ODS('Line: ' & ITS.Lines.OL)
IF F
ITS.ODS(' ' & Format(F,@n_3) & ' Fields from ' & ITS.Lines.OL)
LOOP X = 1 TO F
GET(ITS.CSVFields,X)
ITS.ODS(' ' & Format(X,@n02) & ' ' & ITS.CSVFields.OL)
END
END
END
Parsing a semicolon separated file:
ITS ITStringClass
S STRING(255)
I LONG
F LONG
X LONG
CODE
ITS.FileToLines('testcsv2.csv')
LOOP I = 1 TO Records(ITS.Lines)
GET(ITS.Lines,I)
F = ITS.ParseCSVLine(ITS.Lines.OL,False,';')
ITS.ODS('Line: ' & ITS.Lines.OL)
IF F
ITS.ODS(' ' & Format(F,@n_3) & ' Fields from ' & ITS.Lines.OL)
LOOP X = 1 TO F
GET(ITS.CSVFields,X)
ITS.ODS(' ' & Format(X,@n02) & ' ' & ITS.CSVFields.OL)
END
END
END
See also: