Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request an option of disabling column names of csv output and writing in rowwise structure #1305

Open
YanzhaoW opened this issue Sep 17, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@YanzhaoW
Copy link

YanzhaoW commented Sep 17, 2024

Hi,

I would like to request two features regarding csv writing.

First, it would be really nice if users can disable printing the column names. In some situations, the whole data is not available and new value of the structure are read and written inside an event loop. But when it's transformed to a string, the output string contains the column names each time. But a correct csv file only has the column names once on the top.

For example:

struct CsvStruct {
    std::vector<int> header1 {1, 2, 3};
    std::vector<float> header2 {4., 5, 6};
    std::vector<std::string> header3 {"a", "b", "c"};
};

auto main() -> int {
    auto my_csv = CsvStruct{};
    auto sstream = std::stringstream{};
    auto buffer = std::string{};
    auto ec =
        glz::write<glz::opts{.format = glz::csv, .layout=glz::colwise}>(
            my_csv, buffer);
    sstream << buffer;
    ec =
        glz::write<glz::opts{.format = glz::csv, .layout=glz::colwise}>(
            my_csv, buffer);
    sstream << buffer;
    std::print("{}", sstream.str());
    return 0;
}

outputs a string:

header1,header2,header3
1,4,a
2,5,b
3,6,c
header1,header2,header3
1,4,a
2,5,b
3,6,c

which is an ill-formatted csv file.

The second request is whether we could output to a csv string from a vector of struct. In most of cases, each row in a csv file represents a data point and it's very normal to have something like std::vector<DataPoint>. So it would be greate to have an API like:

struct CsvStruct{
    int header1 = 1;
    float header2 = 2.;
    std::string header3 = "a";
};

auto main() -> int {
    auto my_csv = std::vector<CsvStruct>{};
    my_csv.emplace_back();
    auto buffer = std::string{};
    auto ec =
        glz::write<glz::opts{.format = glz::csv, .layout=glz::rowwise}>(
            my_csv, buffer);
    return 0;
}

Many thanks in advance

@stephenberry
Copy link
Owner

Thanks for your suggestions. I've had an issue for a while about supporting CSVs without column or row keys (#853), so this is extra motivation to get that done.

Your example of std::vector<DataPoint> is also a good suggestion.

I'm not sure when I'll get to these, because I'm making other improvements to Glaze right now. But, I'll keep this issue alive until these features are added.

@stephenberry stephenberry added the enhancement New feature or request label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants