csv-into-map(file a, [mapname b], [keycol(s) c], [csv delim d], [csv encoding e], [clear first f])
Group |
Reads CSV file a (delimiter specified in d), using the local system's encoding (or as specified in parameter e) and writes each line into a named map b, by using c as index of the field to be used as key and the whole line as value. Parameter g ensures Excel compliance. Note: Lines beginning with character # are not read in.
If parameter f is set to true, the map will be cleared before the processing will start.
The key of the map is the CSV field given by the index c, the value is the complete line of the CSV file including the key column(s). If the same key occurs within the CSV file more than once, the last entry overwrites the previous ones.
Composite keys: If you want to build a key out of several columns of the CSV file, you can indicate the columns to be used by their indexes, separated by an arbitrary delimiter, within parameter c. Note that the delimiter has to remain the same between all the indexes and cannot be the delimiter of the CSV file.
The CSV file will be cached. A repeated call of the function will use the cached CSV file for improved performance unless the file has been modified. The cache expires after 30 minutes. In that case, the file will be read again.
After processing the CSV file, the function returns the number of key-value pairs contained in the map.
Description of Parameters
Parameter |
Description |
a |
Filename of the CSV file. |
b |
(optional) Name of the map. Default: default |
c |
(optional) Index of the key column. Default: 1 |
d |
(optional) Delimiter between the values in the CSV file. Default: , |
e |
(optional) The charset the CSV file is encoded. Default: The system encoding. |
f |
(optional) true if the map should be cleared first. Default: true |
g |
(optional) If true, Excel compliance is ensured. If empty, then false (default). |
Examples
The following CSV file is given:
key1_1,key1_2,value1,value2,value3
key2_1,key2_2,value4,value5,value6
Different parameters and results:
Parameter a |
b |
c |
d |
e |
f |
g |
Result |
Values of the Map |
./conf/example/values.csv |
RESMAP |
|
, |
|
|
|
2 |
{key1_1=key1_1,key1_2,value1,value2,value3 key2_1=key2_1,key2_2,value4,value5,value6} |
./conf/example/values.csv |
RESMAP |
2 |
, |
|
true |
|
2 |
{key1_2=key1_1,key1_2,value1,value2,value3 key2_2=key2_1,key2_2,value4,value5,value6} |
./conf/example/values.csv |
RESMAP |
1#2 |
, |
|
true |
|
2 |
{key1_1#key1_2=key1_1,key1_2,value1,value2,value3 key2_1#key2_2=key2_1,key2_2,value4,value5,value6} |