在C ++中获取文本CSV文件中的字符串 [英] Get a string in text CSV file in C++

查看:218
本文介绍了在C ++中获取文本CSV文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型CSV(约75 MB)这种类型:

  1,3,4.1,5.4 
-2,-4,-0.1,-11.3
...

我使用此代码存储数据(C style)

  #include< iostream& 
#include< cstdio>
#include< vector>

int main()
{
int s;
int x;
float y;
double z;

std :: vector< int> t;
std :: vector< int> u;
std :: vector< float> v;
std :: vector< double> w;

if(std :: FILE * f = std :: fopen(data.csv,r)){
while(std :: fscanf(f,%d ,%d,%f,%lf,& s,& x,& y,& z)== 4){
t.push_back
u.push_back(x);
v.push_back(y);
w.push_back(z);
}
std :: fclose(f);
}

return 0;
}

它带走了我这个大型CSV(〜75MB):

  real 0m3.195s 
用户0m3.032s
sys 0m0.148s



在C风格中这么快!



>(C ++ style):

  #include< iostream> 
#include< fstream>
#include< vector>

int main()
{
char c; //去吃逗号。不吃空格:-(
int s;
int x;
float y;
double z;

std :: vector< int> t ;
std :: vector< int> u;
std :: vector< float> v;
std :: vector< double> w;

std: :ifstream file(data.csv);
while(file>>>>>>>>> x>>>>> y> > z){
t.push_back(s);
u.push_back(x);
v.push_back(y);
w.push_back b $ b}

return 0;
}

它花了我这个大型CSV(〜75MB):

  real 0m4.766s 
用户0m4.660s
sys 0m0.088s

C风格更快!



我想读取第一列(或第二个)中的字符串,并放入 std :: string



我尝试了许多可能性(char *,iostream等等),但我不能以快速,优雅的方式做。

大型CSV文件的类型示例(是否比其他文件更容易阅读?):



csv

  hi,3,4.1,5.4 
hello,-4,-0.1, 11.3
...

b.csv : p>

  hi 3 4.1 5.4 
hello -4 -0.1 -11.3
...



c.csv

 hi,3,4.1,5.4 
hello, - 4,-0.1,-11.3
...

d.csv

 hi3 4.1 5.4 
hello-4 -0.1 -11.3
...

非常感谢您的帮助! :)

解决方案

所以你在寻找一种更有效的方法?嗯,你能做的一件事是考虑你是否真的需要向量。根据您的使用情况,您可能会更喜欢某种链接列表结构。


I have a large CSV (~75 MB) of this kind:

1,3,4.1,5.4
-2,-4,-0.1,-11.3
...

And I store my data with this code (C style):

#include <iostream>
#include <cstdio>
#include <vector>

int main()
{
    int s;
    int x;
    float y;
    double z;

    std::vector<int> t;
    std::vector<int> u;
    std::vector<float> v;
    std::vector<double> w;

    if (std::FILE *f = std::fopen("data.csv", "r")) {
        while (std::fscanf(f, "%d,%d,%f,%lf", &s, &x, &y, &z) == 4) {
            t.push_back(s);
            u.push_back(x);
            v.push_back(y);
            w.push_back(z);
        }
    std::fclose(f);
    }

    return 0;
}

And it tooks me in this large CSV (~75MB):

real        0m3.195s
user        0m3.032s
sys         0m0.148s

It's so fast in C style!

Another way it's with this code (C++ style):

#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    char c; // to eat the commas. Not eat spaces :-(
    int s;
    int x;
    float y;
    double z;

    std::vector<int> t;
    std::vector<int> u;
    std::vector<float> v;
    std::vector<double> w;

    std::ifstream file("data.csv");
    while (file >> s >> c >> x >> c >> y >> c >> z) {
        t.push_back(s);
        u.push_back(x);
        v.push_back(y);
        w.push_back(z);
    }

    return 0;
}

And it tooks me in this large CSV (~75MB):

real        0m4.766s
user        0m4.660s
sys         0m0.088s

C style is more fast!

I'd like to read a string in the first column (or in the second) and put into a vector of std::string.

I try many possibilites (char *, iostream, etc.) But I can't done in a fast and elegant way.

Examples of types of large CSV files (is there one easier to read than another?):

a.csv:

hi,3,4.1,5.4
hello,-4,-0.1,-11.3
...

b.csv:

hi 3 4.1 5.4
hello -4 -0.1 -11.3
...

c.csv:

"hi",3,4.1,5.4
"hello",-4,-0.1,-11.3
...

d.csv:

"hi" 3 4.1 5.4
"hello" -4 -0.1 -11.3
...

Thank you very much for the help! :)

解决方案

So you're looking for a more efficient way of doing this? Well, one thing you could do is consider whether or not you really need vectors. Depending on your usage, you may be better with some kind of linked list structure.

这篇关于在C ++中获取文本CSV文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆