How to save these data?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • manuhack

    How to save these data?

    I've written a program in Python to get some data from a website. The
    data structure is as follows. Every day there are new data. For each
    day, there are many stocks. For each stock, there are two columns of
    numbers namely price and volume. The numbers of data point
    (price-volume pair) is not known nor equal for each stock.

    Now the problem is how I should save the data in text (if you think I
    should use database like MySQL, let me know) so that I can use C++
    (with STL) to get the data for analysis. And when I get the data, I
    usually get them stock by stock.

    At the moment, I save the data like this: a folder for each stock is
    made with the name being their stock code. Then two subfolders called
    price and volume are made. Then for each subfolder, a text file named
    by the date contains the price or volume. (For some reasons I didn't
    merge these two in a file with a tab to separate the data)

    But I still think there is a better way to do it. The crucial thing is
    that for each day, I can put the data for all stock in every single day
    in a format like this

    Stock Name (may contain space) or Stock Code \n
    Price \t Volume \n
    ....
    Price \t Volume \n
    Stock Name (may contain space) or Stock Code \n
    Price \t Volume \n
    ....
    Price \t Volume \n
    ....
    Stock Name (may contain space) or Stock Code \n
    Price \t Volume \n
    ....
    Price \t Volume \n

    However, this way I don't know how to extract the data for each stock
    using C++, particularly using ifstream. Any suggestion is welcome!
    Thanks and merry Xmas!

  • BobR

    #2
    Re: How to save these data?


    manuhack wrote in message ...
    >
    >But I still think there is a better way to do it. The crucial thing is
    >that for each day, I can put the data for all stock in every single day
    >in a format like this
    >
    >Stock Name (may contain space) or Stock Code \n
    >Price \t Volume \n
    >...
    >Price \t Volume \n
    >Stock Name (may contain space) or Stock Code \n
    >Price \t Volume \n
    >...
    >
    >However, this way I don't know how to extract the data for each stock
    >using C++, particularly using ifstream. Any suggestion is welcome!
    >Thanks and merry Xmas!
    Load the file, line by line, into a vector. Then parse the strings from
    there.

    // #include <string>, <fstream>, <vector>
    std::ifstream datin( "testdata.t xt" );
    if( not datin.is_open() ){ throw "a fit"; }

    std::vector<std ::stringTmpFile ;
    for( std::string line; std::getline( datin, line ); ){
    TmpFile.push_ba ck( line );
    }

    std::string tmp = TmpFile.at(0);
    // parse 'tmp' using string.substr() and string.find()

    // or std::stringstre am
    int Price(0);
    int Volume(0);
    std::istringstr eam data( TmpFile.at(0) );
    data >Price;
    data >Volume;


    Merry Christmas all!
    --
    Bob R
    POVrookie


    Comment

    • manuhack

      #3
      Re: How to save these data?


      BobR wrote:
      >
      Load the file, line by line, into a vector. Then parse the strings from
      there.
      >
      // #include <string>, <fstream>, <vector>
      std::ifstream datin( "testdata.t xt" );
      if( not datin.is_open() ){ throw "a fit"; }
      >
      std::vector<std ::stringTmpFile ;
      for( std::string line; std::getline( datin, line ); ){
      TmpFile.push_ba ck( line );
      }
      >
      std::string tmp = TmpFile.at(0);
      // parse 'tmp' using string.substr() and string.find()
      >
      // or std::stringstre am
      int Price(0);
      int Volume(0);
      std::istringstr eam data( TmpFile.at(0) );
      data >Price;
      data >Volume;
      Now if I have a line with
      StockCode \t Stock name (may have space) \n
      I put the whole line in a stringstream. Then I recover the stock code
      by using >>. However, I've checked a few books looking for how I could
      modify >so that I can use >to get the stock name in a string (not
      just the name till the first space). Or is there a better way to do it?

      Comment

      • BobR

        #4
        Re: How to save these data?


        manuhack wrote in message ...
        >
        >Now if I have a line with
        >StockCode \t Stock name (may have space) \n
        >I put the whole line in a stringstream. Then I recover the stock code
        >by using >>. However, I've checked a few books looking for how I could
        >modify >so that I can use >to get the stock name in a string (not
        >just the name till the first space). Or is there a better way to do it?
        >
        {
        std::istringstr eam SimFileIn( "StockCode \t Stock name (may have space)
        \n" );
        std::string Code;
        std::getline( SimFileIn, Code, '\t' );
        SimFileIn.ignor e(1); // skip space
        std::string Name;
        std::getline( SimFileIn, Name ); // defaults to '\n'
        std::cout<< Code <<std::endl;
        std::cout<< Name <<std::endl;
        }
        /* - output -
        StockCode
        Stock name (may have space)
        */

        --
        Bob R
        POVrookie


        Comment

        • manuhack

          #5
          Re: How to save these data?


          BobR wrote:
          >
          {
          std::istringstr eam SimFileIn( "StockCode \t Stock name (may have space)
          \n" );
          std::string Code;
          std::getline( SimFileIn, Code, '\t' );
          SimFileIn.ignor e(1); // skip space
          std::string Name;
          std::getline( SimFileIn, Name ); // defaults to '\n'
          std::cout<< Code <<std::endl;
          std::cout<< Name <<std::endl;
          }
          /* - output -
          StockCode
          Stock name (may have space)
          */
          Does that mean it's not possible to use the operator >to input the
          name and code to Name and Code from the stringstream instead of using
          getline?

          Comment

          • BobR

            #6
            Re: How to save these data?


            manuhack wrote in message ...
            >
            >BobR wrote:
            >{
            > std::istringstr eam SimFileIn( "StockCode \t Stock name (may have
            space)
            >\n" );
            > std::string Code;
            > std::getline( SimFileIn, Code, '\t' );
            > SimFileIn.ignor e(1); // skip space
            > std::string Name;
            > std::getline( SimFileIn, Name ); // defaults to '\n'
            > std::cout<< Code <<std::endl;
            > std::cout<< Name <<std::endl;
            >}
            >/* - output -
            >StockCode
            >Stock name (may have space)
            >*/
            >
            >Does that mean it's not possible to use the operator >to input the
            >name and code to Name and Code from the stringstream instead of using
            >getline?
            If you are served a bowl of broth, do you use a fork or a spoon to eat it?
            Use the proper tool for the job. What is your obsession with the operator>>?


            class Stock{ public: // or struct
            std::string Code;
            std::string Name;
            friend std::istream& operator>>( std::istream &input, Stock &St){
            std::getline( input, St.Code, '\t' );
            if( ' ' == input.peek() ){ input.ignore(); } // skip a space
            std::getline( input, St.Name ); // defaults to '\n'
            return input;
            } // end operator>>(istr eam&, Stock&)
            }; // class Stock

            int main(){
            std::istringstr eam SimFileIn( "StockCode \t Stock name (may have space)
            \n" );
            Stock St1;
            SimFileIn >St1;
            cout<< St1.Code <<std::endl;
            cout<< St1.Name <<std::endl;
            }
            /* - output -
            StockCode
            Stock name (may have space)
            */

            --
            Bob R
            POVrookie


            Comment

            Working...