GithubHelp home page GithubHelp logo

elongame / litecsvparser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from floatinghotpot/litecsvparser

0.0 1.0 0.0 140 KB

A lite CSV reader and writer in C#, without any heavy dependency

C# 100.00%

litecsvparser's Introduction

LiteCsvParser

A lite CSV reader and writer in C#, without any heavy dependency.

Purpose

Parsing CSV files may sound like an easy task, but in reality it is not that trivial.

  • Using String.Split() is fast, but it cannot handle quoting notation and eascaping.

  • Using Microsoft.VisualBasic.FileIO is very good, but it's dependent on Windows platform.

If you wanna read/write CSV file in Mono/Unity cross-platform project, LiteCsvParser is the right choice.

How to Use

You will need only the file "LiteCsvParser.cs", copy & ad it into your own project.

You can also git clone this project, use MonDevelop or VisualStudio to open the project to build a demo executable.

Compatibility

  • VisualStudio
  • Mono
  • Unity

See dependency:

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

Example

Read & write all data grid:

using LiteCsvParser;

List<List<string>> dataGrid = CsvFileReader.ReadAll("test.csv", Encoding.GetEncoding("gbk"));

// TODO: deal with data grid
foreach(var row in dataGrid) {
	foreach(var cell in row) {
		Console.Write("\t\t" + cell);
	}
	Console.Write("\n");
}

CsvFileWriter.WriteAll(dataGrid, "output2.csv", Encoding.GetEncoding("gbk"));

Read row by row (if the CSV file is very large):

	List<string> row = new List<string>();
	using (var reader = new CsvFileReader("Test.csv"))
	{
		while (reader.ReadRow(row))
		{
			// TODO: Do something with columns' values
			foreach(string cell in row) {
				Console.Write("\t\t" + cell);
			}
			Console.Write("\n");
		}
	}

Write row by row:

	using (var writer = new CsvFileWriter("Test.csv"))
	{
		// Write each row of data
		for (int i = 0; i < 10; i++)
		{
			List<string> row = new List<string>();
			
			// TODO: Populate column values for this row
			for(int j=0; j<5; j++) {
				row.Add("" + j);
			}

			writer.WriteRow(row);
		}
	}

Credits

The source code was originally written by Jonathan Wood, from blackbetcoder.com, but not found on github.com, so I create a project in github to maintain it.

This project is published under MIT License. From Jonathan Wood: Use of this article and any related source code or other files is governed by the terms and conditions of The Code Project Open License.

Feedback and pull requests are welcome.

litecsvparser's People

Contributors

floatinghotpot avatar

Watchers

James Cloos 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.