CSVReader

From Unify Community Wiki
Revision as of 19:50, 24 August 2011 by Dock (Talk | contribs)

Jump to: navigation, search


Author: Dock : This is my first time adding a script the Unity wiki :)


Contents

Info

This is just a simple CSVReader for reading 'Comma Separated Value' text output. It should also work with Tab separated values. It uses Regex to do some of the splitting.

Usage

Just stick it in your plugins folder and you can access it from JS or C#.

CSVReader.cs

<csharp>/* CSVReader by Dock. (24/8/11) http://starfruitgames.com

usage: CSVReader.SplitCsvGrid(textString)

returns a 2D string array.

Drag onto a gameobject for a demo of CSV parsing.

  • /

using UnityEngine; using System.Collections; using System.Linq;

public class CSVReader : MonoBehaviour { public TextAsset csvFile; public void Start() { string[,] grid = SplitCsvGrid(csvFile.text); Debug.Log("size = " + (1+ grid.GetUpperBound(0)) + "," + (1 + grid.GetUpperBound(1)));

DebugOutputGrid(grid); }

// outputs the content of a 2D array, useful for checking the importer static public void DebugOutputGrid(string[,] grid) { string textOutput = ""; for (int y = 0; y < grid.GetUpperBound(1); y++) { for (int x = 0; x < grid.GetUpperBound(0); x++) {

textOutput += grid[x,y]; textOutput += "|"; } textOutput += "\n"; } Debug.Log(textOutput); }

// splits a CSV file into a 2D string array static public string[,] SplitCsvGrid(string csvText) { string[] lines = csvText.Split("\n"[0]);

int width = 0; for (int i = 0; i < lines.Length; i++) { string[] row = SplitCsvLine( lines[i] ); width = Mathf.Max(width, row.Length + 1); }

string[,] outputGrid = new string[width, lines.Length]; for (int y = 0; y < lines.Length; y++) { string[] row = SplitCsvLine( lines[y] ); for (int x = 0; x < row.Length; x++) { outputGrid[x,y] = row[x];

// This line was to replace "" with " in my output. // Include or edit it as you wish. outputGrid[x,y] = outputGrid[x,y].Replace("\"\"", "\""); } }

return outputGrid; }

// splits a CSV row static public string[] SplitCsvLine(string line) { return (from System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(line, @"(((?<x>(?=[,\r\n]+))|""(?<x>([^""]|"""")+)""|(?<x>[^,\r\n]+)),?)", System.Text.RegularExpressions.RegexOptions.ExplicitCapture) select m.Groups[1].Value).ToArray(); } } </csharp>

ReadDemo.cs

<csharp>using UnityEngine; using System.Collections; using System.Collections.Generic;

public class ReadDemo : MonoBehaviour {

public TextAsset csv;

void Start () { CSVReader.DebugOutputGrid( CSVReader.SplitCsvGrid(csv.text) ); } }

</csharp>

ReadDemo.js

<javascript>var csv : TextAsset;

function Start () { CSVReader.DebugOutputGrid( CSVReader.SplitCsvGrid(csv.text) ); } </javascript>

ExampleCSV.txt

(has to be .txt for Unity to recognise it as a textasset) <javascript>Flavours,Strawberry,Banana,Apple,Owl Prices,0.34,0.30,0.60,34.0 Sadness,0,0,1,99</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox