How to convert a tabular format or python datafram

2019-08-18 18:15发布

问题:

(Please Note: I am not able to embed images here. I dont have enough points for that. Could anyone please help me with that.)

I understand how to convert the struct corresponding to the following tablular format (Struct1) into msgpack format:

Struct1

For this I use the following code:

#include <sstream>
#include <iostream>
#include <msgpack.hpp>

inline std::ostream& hex_dump(std::ostream& o, std::string const& v) {
    std::ios::fmtflags f(o.flags());
    o << std::hex;
    o << "b\'";
    for (auto c : v) {
        o << "\\x" << std::setw(2) << std::setfill('0') << (static_cast<int>(c) & 0xff);
    }
    o << "\'";
    o.flags(f);
    return o;
}

struct your_type {
    int a;
    int b;
    MSGPACK_DEFINE(a, b);
};

int main() {
    // packing
    std::stringstream ss;
    std::stringstream sshex;
    std::string ssnew;
    std::vector<std::map<std::string, your_type>> v
    {
        {
            { "t",{ 1, 2} }

        },
        //{
            { "value",{6, 5 } }
        }
    };

    msgpack::pack(ss, v);


    auto oh = msgpack::unpack(ss.str().data(), ss.str().size());
    // JSON output
    std::cout << oh.get() << std::endl;

    std::cout << std::endl;
    // hex dump output
    hex_dump(sshex, ss.str()) << std::endl;

    std::cout << sshex.str();

    ssnew = sshex.str();

    return ssnew;

}

Now I would want to try the following to be converted in the same format:

I want to add each row in a loop, pass the values through the loop to the structure and then convert into msgpack format. Again, repeat the process after adding another row. I do not want to statically define the values as before. For eg:

First time - data passed for conversion (Struct2):

Struct2

Second time - data passed for conversion (Struct3): Struct3

I have tried with using int for t and int array for val. I am not able to proceed further. I have researched msgpack library but in vain. Could anyone please help me as how can I proceed with this? Even a little guidance would be a lot helpful.