GithubHelp home page GithubHelp logo

uniquebui / csvstream Goto Github PK

View Code? Open in Web Editor NEW

This project forked from awdeorio/csvstream

0.0 0.0 0.0 39 KB

An easy-to-use CSV file parser for C++

License: MIT License

Makefile 34.01% C++ 65.99%

csvstream's Introduction

csvstream

An easy-to-use CSV file parser for C++

Andrew DeOrio [email protected]
http://andrewdeorio.com

Quick start

$ git clone https://github.com/awdeorio/csvstream.git
$ cd csvstream/
$ make test

Example 1

This example reads one column from a CSV file.

// csvstream_example1.cpp
#include "csvstream.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;


int main() {
  // Open file
  csvstream csvin("csvstream_example.csv");

  // Rows have key = column name, value = cell datum
  map<string, string> row;

  // Extract the "animal" column
  while (csvin >> row) {
    cout << row["animal"] << "\n";
  }

}

Input

$ cat csvstream_example.csv
name,animal
Fergie,horse
Myrtle II,chicken
Oscar,cat

Compile

$ make csvstream_example1
  # OR
$ g++ -std=c++11 csvstream_example1.cpp -o csvstream_example1

Output

$ ./csvstream_example1
horse
chicken
cat

Example 2

This example has an outer loop for each row and an inner loop for each column.

//csvstream_example2.cpp
#include "csvstream.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;


int main() {
  // Open file
  csvstream csvin("csvstream_example.csv");

  // A row is a map<string, string>, key = column name, value = datum
  map<string, string> row;

  // Read file
  while (csvin >> row) {
    cout << "row:" << "\n";
    for (auto &col:row) {
      const string &column_name = col.first;
      const string &datum = col.second;
      cout << "  " << column_name << ": " << datum << "\n";
    }
  }

}

Input

$ cat csvstream_example.csv
name,animal
Fergie,horse
Myrtle II,chicken
Oscar,cat

Compile

$ make csvstream_example2
  # OR
$ g++ -std=c++11 csvstream_example2.cpp -o csvstream_example2

Output

$ ./csvstream_example2
row:
  animal: horse
  name: Fergie
row:
  animal: chicken
  name: Myrtle II
row:
  animal: cat
  name: Oscar

Example 3

This example uses a vector-of-pair to keep the order of the items read from the file.

/* csvstream_example3.cpp
 *
 * Andrew DeOrio <[email protected]>
 *
 * An easy-to-use CSV file parser for C++
 * https://github.com/awdeorio/csvstream
 */

#include "csvstream.h"
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;


int main() {
  // Open file
  csvstream csvin("csvstream_example.csv");

  // A row is a vector<pair<string, string>>
  // key = column name, value = cell datum
  vector<pair<string, string>> row;

  // Read file
  while (csvin >> row) {
    cout << "row:" << "\n";
    for (auto &col:row) {
      const string &column_name = col.first;
      const string &datum = col.second;
      cout << "  " << column_name << ": " << datum << "\n";
    }
  }

}

csvstream's People

Contributors

awdeorio avatar campisano avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.