Forum Discussion

FICO's avatar
FICO
Community Manager
6 years ago

Reading CSV files

I'm not quite sure what it is that I'm doing wrong here, but I've been getting "SHEET: Wrong number of columns (req:3 found:2)" whenever I try to load data from a csv/xlsx file.

To elaborate upon the issue, I've been trying to create a new problem based on h3portf.mos and h3portf.dat in the following link by changing variable names and adding more rows to the data:

https://examples.xpress.fico.com/example.pl?id=mosel_app_8_10

I do realize that the example data files are in .dat format, but since I'll be doing optimization problems on a much larger scale than this I created a new data file in Excel.

FICOAs the columns are of different lengths I wrote the initializations code in the following manner:

initializations from "mmsheet.csv:"+FILENAME

CAPITAL as "[A2]"

RETURNS as "[A5:A34]"

US_STOCKS as "[B5:B19]"

BANKS as "[C5:C13]"

end-initializations

When I kept getting the aforementioned error message I tried the R*C* method to the same result. Any assistance would be very much appreciated.

2 Replies

  • FICO's avatar
    FICO
    Community Manager

    When reading data arrays from spreadsheet the default setting assumes that the selected input range contains the required index values: this is not the case in your example. You therefore need to work with the 'noindex' option:

    RETURNS as "noindex;[A5:A34]"

    This obviously means that the index range needs to have been stated in the model before you read in the data. For a more flexible setup it would probably be better to remove this definition from the data and add an index column to your spreadsheet file, so the model should just have the declaration

    SHARES: range

    and you either add an additional first column with index values for the array to your spredsheet, or you can work with the option 'autondx' to generate the indices automatically:

    RETURNS as "autondx;[A5:A34]"

    (Note that the other data items are either scalar values or sets, so the remark about indices does not apply to them.)

    One further suggestion: to make the data input more flexible you could also reorganize the format of your spreadsheet so that each column corresponds a one data item, where the first row contains the headers, then data values in the rest of the respective column, in which case you can simply read the entire columns (using the option 'skiph' to ignore the header line), for example:

    initializations from 'mmsheet.csv:skiph;h3portf2.csv'
      RETURNS as "autondx;[A:A]"    ! Header in A1, data values starting in A2
      CAPITAL as "[E:E]"     ! Assuming that this value is now in field E2
      US_STOCKS as "[B:B]"
      BANKS as "[C:C]"
     end-initializations
  • FICO's avatar
    FICO
    Community Manager

    Thank you so much @Susanne Heipcke​, your answer was just the thing I needed!