GithubHelp home page GithubHelp logo

adamjliang / csvstream Goto Github PK

View Code? Open in Web Editor NEW

This project forked from awdeorio/csvstream

0.0 0.0 0.0 97 KB

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

License: MIT License

Makefile 29.35% C++ 70.65%

csvstream's Introduction

csvstream

Build Status

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

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

Table of Contents

Quick start

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

Example 1: Read one column

This example reads one column from a CSV file.

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


int main() {
  // Open file
  csvstream csvin("input.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 input.csv
name,animal
Fergie,horse
Myrtle II,chicken
Oscar,cat

Compile

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

Output

$ ./example1
horse
chicken
cat

Example 2: Read each row and each column with nested loops

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

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


int main() {
  // Open file
  csvstream csvin("input.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 input.csv
name,animal
Fergie,horse
Myrtle II,chicken
Oscar,cat

Compile

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

Output. Notice output order within each row is animal followed by name. This is because iterating over a map yields items in sorted order by key.

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

Example 3: Maintaining order of columns in each row

This example uses a vector-of-pair to maintain the order of values read from each row.

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


int main() {
  // Open file
  csvstream csvin("input.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 (unsigned int i=0; i < row.size(); ++i) {
      const string &column_name = row[i].first;
      const string &datum = row[i].second;
      cout << "  " << column_name << ": " << datum << "\n";
    }
  }

}

Input

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

Compile

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

Output. Notice output order within each row is name followed by animal. This is the order that the columns appear in the CSV file.

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

Changing the delimiter

By default, values in a row are delimited by a comma ,. Change the delimiter with the delimiter constructor parameter.

This example changes the delimiter to the | character.

csvstream csvin("input.csv", '|');

Allow too many or too few values in a row

By default, if a row has too many or too few values, csvstream raises and exception. With strict mode disabled, it will ignore extra values and set missing values to empty string. You must specify a delimited when using strict mode.

csvstream csvin("input.csv", ',', false);

csvstream's People

Contributors

awdeorio avatar fpohtmeh-old 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.