Promotic
WikipediaLinkedInYoutubeTwitterFacebook

FileCsvRead - method of object Pm

Description:
Parameterized reading the data from a CSV file.
Syntax:
FileCsvRead(sFile As String, sFields As String, sParams As String) As Variant
Calling:
data = Pm.FileCsvRead(sFile, sFields, sParams)
Parameters:
sFile(String) File to read. Independently whether this parameter is set or not, it is possible to open the window for choosing a disk file by setting the fileselect parameter. If a full path is not entered, it is completed relatively according to the application file folder.

It is recommended to use the PROMOTIC path syntax - see PROMOTIC path to files and folders.

sFields(String) Specifies which columns will be read from the file and of which data types the read data will be. For example "filecols:0,1,2,3;datatypes:string,double,string,string;".
filecols - Defines which columns of the source CSV file will be included into the output array of values. If filecols:all; (default) is set then all columns are included in the same order.

List of numeric indexes - each column of the output array of values has the index (order) in the source CSV file. Each column of the output array can be read from any column of the source CSV file. Specifies which columns from the file will be read.

If filecols:all; is set (default), then all columns are read.

If, for example, filecols:0,1,3,8; is set, then only those columns from the file that are stated here by the index (zero-based index) are read.

datatypes - al ist of column data types of the output value array. The column data type defines the way of reading data of the corresponding column of the CSV file. The defined column data types must correspond with the output data array. This means that the list must contain as many items as is the number of columns of the output value array. The columns in the source CSV file may differ from the output value array, if the filecols column filter is used. Available data types of columns: string, byte, integer, bool, single, double. For example, datatypes:string,integer,bool,single;

If the parameter is not stated, then the default type is string.

sParams(String) Specifies the way or reading the data from the file.. For example "fileselect:yes;colnameheader:yes;delimiter:comma;"
"fileselect:xxx;" (optional)
yes - by calling the method the window for choosing a disk file is opened independently on setting the sFile parameter.
no (default)
"colnameheader:xxx;" (optional)
yes - in the first row of the file there are no data but names of individual columns.
no (default) - already in the first row of the file the data are saved.
"swap:xxx;" (optional)
yes (default) - columns from the file are saved as rows in the resulting matrix (in the data(i,j) cell there is the value of the i-th column and j-th row in the file).
no - rows from the file are saved as rows in the resulting matrix (in the data(i,j) cell there is the value of the i-th row and j-th column in the file).
"delimiter:xxx;" (optional) - (only for CSV) The separator of values in the CSV file. The allowed values are:
semicolon (default) - the delimiter is a semicolon.
comma - the delimiter is a comma.
space - the delimiter is a space.
tab - the delimiter is a tab.
number - i.e. the separator is defined as a decimal number of the ASCII character, for example "delimiter:124;" represents the "|" separator (vertical line).
"lastcolsep:xxx;" (optional)
yes - When writing, the separator is added after the last value on the row, when reading, the empty value at the end of each row is ignored. From the CSV file standard point of view, this is not correct.
no (default) - The separators are present only between values on the same row, not at the end. From the CSV file standard point of view, this is correct.
Return Values:
- Returns the matrix (i.e. 2-dimensional Array) if more than one column is read from the file.
- Returns the vector (i.e. 1-dimensional Array) if only one column is read from the file.
- Returns the value of the Empty type if reading the data failed.
Note:
The FileTextWrite method can be used for creating and writing the data into a CSV file. The FileTextWrite method can also be used (see Example2 of this method).
 
This method is not functional for Web Panels.
Example1:
The method displays the window for choosing a file (the preset file is data.csv stored in the application folder), reads the data of the selected file from all columns. The values in the file are separated by semicolon and the file doesn't include the column names in the first row.
Dim vData
vData = Pm.FileCsvRead("#app:data.csv", "filecols:all;", "delimiter:semicolon;colnameheader:no;fileselect:yes;")
If Pm.IsValid(vData) Then
  ... 'When the data were read successfully.
End If
Example2:
The method reads the data from the file data.csv (name,value,unit,note), stored in the application folder. It reads three columns with swapped order (name,note,value, i.e. columns 0,3,1) and the result will be stored into the vData array. The values in the output array are of the following data types string, string and double. The values in the file are separated by semicolon and the file does not include the column names in the first row.
Dim sParams, vData
sParams = "delimiter:semicolon;colnameheader:no;"
vData = Pm.FileCsvRead("#app:data.csv", "filecols:0,3,1;datatypes:string,string,double;", sParams)
If Pm.IsValid(vData) Then
  ... 'When the data were read successfully.
End If
Navigation:
 
- Pm
 
- Abs
- Cos
- E
- Exp
- FileCsvRead
 
 
- LN2
- PI
- Pow
- Sin
- Tan
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice