How To

How To - Read and Write CSV Files (updated)

  • 29 July 2019
  • 2 replies
  • 227 views
How To - Read and Write CSV Files (updated)
Userlevel 4
Badge +6
The How To 'Read and Write CSV Files' has been updated.




CSV files are a de facto standard for communication of data. AIMMS provides the libraries
code:
DataLink
and
code:
CSVProvider
in order to serialize CSV files.
This article shows an example of how to read data from a CSV file.

2 replies

Userlevel 5
Badge +5

@milad_37 

Replace s_datatable with EP_datatable in your DataMap statement. You added EP_datatable as a new element to the set S_datatable, and assigned S_datatable to the set dl::DataTables. In the DataMap statement, dl::dt refers to an index/element in the set dl::DataTables, so when you provide S_datatable to that argument - you are referring to the entire set itself and not an index/element in the set. 

 

DataMap(dl::dt,dl::idn,dl::cn,dl::dn) :=  data {

    ( S_datatable, sLocs, 1, 1 ) : "location",

    ( S_datatable, sProd, 2, 2 ) : "product",

    ( S_datatable, pDem , 3, 0 ) : "demand", 

    ( S_datatable, spCmt, 4, 0 ) : "comment"

};

to

DataMap(dl::dt,dl::idn,dl::cn,dl::dn) :=  data {

    ( EP_datatable, sLocs, 1, 1 ) : "location",

    ( EP_datatable, sProd, 2, 2 ) : "product",

    ( EP_datatable, pDem , 3, 0 ) : "demand", 

    ( EP_datatable, spCmt, 4, 0 ) : "comment"

};

Hi,

I’m new to AIMMS and I’m trying to read multiple csv files with the file name dynamically updates.

I created a procedure as follows:

Empty data_declarations ;

SetElementAdd(S_datatable, EP_datatable, SP_filename);

dl::DataTables := S_datatable ;

DataMap(dl::dt,dl::idn,dl::cn,dl::dn) := data {
( S_datatable, sLocs, 1, 1 ) : "location",
( S_datatable, sProd, 2, 2 ) : "product",
( S_datatable, pDem , 3, 0 ) : "demand",
( S_datatable, spCmt, 4, 0 ) : "comment"
};
dl::RemoveDataSourceMapping("TheMapping");
dl::AddDataSourceMapping("TheMapping", DataMap, dl::DependEmpty, dl::TableAttributesEmpty, dl::ColAttributeEmpty);

spCommunicationAttributes :=
{ 'DataProvider' : csvprov::DataLink ,
'ContainsHeaders' : "yes"
! Note that Separator attribute is left to its default of ",".
};

dl::DataRead("komma", ! reading from data source "komma" - because we use csvprovider this data source is a folder.
"TheMapping" , ! using relation "TheMapping" between folder komma and AIMMS identifiers.
spCommunicationAttributes); ! Technicalities on how to communicate.

Also I created a set and string parameter as instructed by tutorial.
 

Set S_datatable {
SubsetOf: dl::DataTables;
Parameter: EP_datatable;
}

StringParameter SP_filename;

But when I run the procedure, it gives me the following error:

The element S_datatable is not in the range set of the running index dt.

 

Can you please help resolve this issue.

 

Thanks

Reply


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator