Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by marsfan · Sep 01, 2014 at 05:51 PM · javascriptimportmappingreadcsv

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Sep 01, 2014 at 06:16 PM 1
Share

Yes, it's possible. Is that all you wanted to know?

avatar image marsfan · Sep 01, 2014 at 11:22 PM 0
Share

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?

avatar image robertbu · Sep 02, 2014 at 12:24 AM 1
Share

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.

avatar image marsfan · Sep 02, 2014 at 10:07 PM 0
Share

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).

avatar image IvovdMarel · Sep 03, 2014 at 04:49 PM 1
Share

$$anonymous$$aybe save yourself some time and spend $2 on CSV Viewer (Asset Store)?

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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);
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marsfan · Sep 06, 2014 at 04:03 PM 0
Share

What does [0] do after .Split?

avatar image marsfan · Sep 06, 2014 at 04:30 PM 0
Share

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.

avatar image robertbu · Sep 06, 2014 at 08:34 PM 1
Share

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.

avatar image gwizdkey · Oct 20, 2016 at 08:37 AM 4
Share

You can use: Split(','); ins$$anonymous$$d of: Split(","[0]);

It's because method expects char, not string.

avatar image
1

Answer by gaminggal39 · Nov 13, 2018 at 09:48 AM

Read Data from CSV file watch this : https://youtu.be/xwnL4meq-j8

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RCBGames · Mar 18, 2020 at 06:17 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges