- Home /
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.
I updated the phrasing to reflect my question. I would like to know how to do it. Specifically the part where I read the file. The rest is plugging the values into variables, right?
Only a tiny corner of this problem is related to Unity. $$anonymous$$ost of the information is generic and can be found numerous places on the net.
How to get the text/where to store the file. This is platform dependent, and you did not specify a platrorm. For example on a PC, you can put the file pretty much anywhere, but a web build can only read a file from the server. Other platforms you have are limited on where you can read/write. Remember your scanner has to be able to write to the same place your app can read from.
Synchronization - you don't want you app trying to read from a file at the same time as the scanner is writing.
Breaking the app into lines. String.Split() will do the job. Remember to research you line endings and/or trim your white space.
Break each line into individual values. Again, String.Split() will do the job.
Converting the values from strings to floats. float.TryParse() will do the job.
How do I do this, I would just like a simple js code that takes a csv file and saves the values to variables (x,z,roty,dist).
$$anonymous$$aybe save yourself some time and spend $2 on CSV Viewer (Asset Store)?
Answer by robertbu · Sep 03, 2014 at 05:28 AM
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("\n"[0]);
var lineData : String[] = (lines[0].Trim()).Split(","[0]);
var x : float;
float.TryParse(lineData[0], x);
Ok. So I cannot seem to print out any of this data. If I run a debug.log of the x value. It return 0, while the first value in the csv file is not 0.
You need to test. Put Debug.Log() statements after the ReadAllText(), another to test lines[0], and for lineData[0]. If they all look fine, check their length to see if there are any hidden characters in the string.
You can use: Split(','); ins$$anonymous$$d of: Split(","[0]);
It's because method expects char, not string.
Answer by gaminggal39 · Nov 13, 2018 at 09:48 AM
Read Data from CSV file watch this : https://youtu.be/xwnL4meq-j8
Answer by Kermer · Jan 10, 2019 at 04:21 PM
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: https://github.com/JoshClose/CsvHelper
Answer by tiagoperes · Oct 23, 2019 at 10:37 AM
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.
Sorry but I'm having an error and I don't know why. For some reson, using this CSV reader, data.Count is equals to 0. Do you know why is this happening? I didn't change anything on the scripts. It should work like this, isn't it? Thank you.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
AssetPostprocessor - Can't change Mesh Data 2 Answers
Different language scripts - how to avoid trouble? 1 Answer
Import .swc file 0 Answers
Font problem The font Assets/Fonts/IMPACT.TTF could not be imported because it couldn't be read 1 Answer