Reading Data from a CSV file

Hello,

I would like to know how to read data from a CSV file, specifically a distance value, x and z coordinates, and a rotation value (y), then enter those into the software.
The purpose of this is to take data from a scanner and create a map of obstacles for it.
Also, is it possible to do this if the CSV file is constantly being updated by the software writing it.

An example is
Scanner is at (3,2), sensor is rotated 142 degrees, item detected 105cm from sensor.
Software adds block 105cm from (3,2)
process is repeated as scanner adds values to the file.

Let me know if anything needs to be clarified.

Also, I only code in javascript.

You are asking someone to write a script for you (or at least a function). This list answers single, specific questions to help you write your own code. Here is a bit of untested code to get you started. It reads all the contents of a file into a single string, splits that string into an array of lines, take the first line and splits it on commas, and converts the first value in the spit array to a float. This is example code that assumes perfectly formatted, numeric only data (no error checking).

	var fileData : String  = System.IO.File.ReadAllText(path)
    var lines : String[] = fileData.Split("

“[0]);
var lineData : String = (lines[0].Trim()).Split(”,"[0]);
var x : float;
float.TryParse(lineData[0], x);

Read Data from CSV file watch this : Read data from CSV file in unity - YouTube

Since gaminggal already performed decent necromancy, I’d like to add few words.

It looks to me like, there’s no decent Asset/Script in Unity to handle CSV, I mean doing proper reading and writing. Most examples given by the community assume that the CSV only uses 2 special symbols which is comma and newline (just LF), which is false. This is fine for learning what CSV is, but you can very easily break your parsing functionality by just editing CSV file with MS Excel, and placing newline anywhere.

Lack of proper implementation might be caused because C# (.Net) already contains large amount of specialized libraries for CSV handling.

If you’re looking for decent CSV reader/writer, you can take a look here:

This is my favorite CSVReader, created by Teemu Ikonen. Basically it goes through the regular expressions, reads the CSV file, and finally converts it as a dictionary for further usage.
Very easy to use and he explains in his blog how to.

public static class CSVParser
{
    static StringReader stringReader;

    /// <summary>
    /// Converts all CSV datafile's lines into a list of string arrays.
    /// </summary>
    public static List<string[]> GetDataListFromCSV(TextAsset csv)
    {
        stringReader = new StringReader(csv.text);
        List<string[]> data = new List<string[]>();
        string splitStr = ",";

        while (stringReader.Peek() != -1)
        {
            string line = stringReader.ReadLine();
            string[] items = line.Split(splitStr.ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
            data.Add(items);
        }
        return data;
    }

    /// <summary>
    /// Converts the list of string arrays parsed from a csv into a dictionary."
    /// </summary>
    public static Dictionary<string, string[]> GetDictionaryFromDataList(List<string[]> data)
    {
        // string dictionary based on line 0 keys, that has an array of items.
        Dictionary<string, string[]> datadic = new Dictionary<string, string[]>();

        for (int i = 0; i < data[0].Length; i++)
        {
            // array of items is size of data list minus 1 to account for line 0 which is keys. 
            string[] items = new string[data.Count - 1];

            // start at line 1, add item to array from data list j, i
            for (int j = 1; j < data.Count - 1; j++)
            {
                items[j - 1] = data[j][i];
            }
            
            // create a dictionary entry with keys from line 0, value items
            datadic.Add(data[0][i], items);
        }

        return datadic;
    }
}

I wrote this up quickly, do note that this worked for extracting values from a very large set of data that I wanted, but I then decided to use something else. So I haven’t actually tested this with small amount of testable data and verified that everything is parsed out correctly because I need to move on to other tasks, if there’s an error it’s easy to fix. Reply.