-2

I am pulling in data from a .csv file. I want only data from column E, but this is conditional on what is specified via column A.

That is, I want to pull the double (type) values given that column A says "USD Libor/Swap".

As of right now it just outputs every line. I've attached a picture of the Excel .csv file for you to see for reference.click here for image

How can you do this? Thanks

string line;

ifstream ycratel; //create the stream object

ycratel.open("YC Rate Levels.csv");

if (ycratel.is_open())
{
    while (getline(ycratel,line))
    {
        getline(ycratel, line);
        cout << line << endl;
    }

    ycratel.close();
}

else cout << "Unable to open file.";
user5619709
  • 53
  • 2
  • 11

1 Answers1

0

The std::getline function is very useful in these cases:

std::string field_text;
std::getline(ycratel, field_text, ','); // Read until ','

Combine with an if statement:

if (field_text == "USD Libor/Swap")
{

Skip 4 columns

std::getline(ycratel, field_text, ','); // field 2
std::getline(ycratel, field_text, ','); // field 3
std::getline(ycratel, field_text, ','); // field 4

Read the 5th column

std::getline(ycratel, field_text);
}

Evaluation of the field text is left as an exercise for the OP.

Note: Since there are only 5 fields, a for loop may be more complicated then unrolling the loop.

This answer assumes file is CSV - Comma Separated Values. The delimiter field of std::getline can be adjusted for other delimiters.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Thanks, this is good stuff. Though, I am not certain how you "skip" columns there. It still prints out everything, even with the IF statement. – user5619709 Aug 30 '16 at 19:46
  • while (getline(ycratel,line)) { if (line == "USD LIBOR/Swap") { getline(ycratel, line, ','); // field 2 getline(ycratel, line, ','); // field 3 getline(ycratel, line, ','); // field 4 getline(ycratel, line); } cout << line << endl; } – user5619709 Aug 30 '16 at 19:47
  • Given that this is csv file, it reads "1,2,abc, zyx, 3, 5", as one big string. It does work in the sense that you can just say "USD LIBOR/Swap" unforunately, since it's a subset. – user5619709 Aug 30 '16 at 20:47
  • Have you tried using the *delimiter" form of `std::getline`? Try writing a small program that reads one line using the delimiter forms of `std::getline`, using 5 different string variables. Print out the values of the variables. – Thomas Matthews Aug 30 '16 at 20:49
  • I was able to figure it out and get the values separated and displayed correctly. Your approach worked. I just didn't realize that blanks in the CSV were considered commas and hence my long frustration. I had to create 17 skipped fields to get the output I wanted. But yes, now I need to convert the 5th column values to double and put them into a vector. Also, I have yet to still figure out how to get the IF statement to work. I put the IF statement after the getline(ycratel,f1,',') and tested if f1 = "USD LIBOR/Swap" and then skipped the rest of the fields like you said. But nothing shows up. – user5619709 Aug 30 '16 at 21:46
  • You need to export the file using commas or tabs as delimiters. Otherwise the first text field is going to cause problems. You may want to have the text fields enclosed between double quotes. Again, try to have the file exported using some other delimiter than space. – Thomas Matthews Aug 30 '16 at 22:46