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 /
This question was closed Feb 23 at 06:08 PM by mjonasz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mjonasz · Feb 04 at 03:49 PM · csv

Why do I get a length of 1 when getting data from a CSV file?

Why does the following script fail to get data from a CSV file? When I debug, it gives me a length of 1, whereas there are over forty lines in the CSV. When I attach the script to Unity, it skips every line past the "for" statement (I assume this is because it keeps reading the length as 1). I pasted the first few lines from the CSV after the script.

 string[] data = scenarioInfoTextFile.text.Split(new char[] { '\n' });
             Debug.Log(data.Length);
             for (int i = 1; i < data.Length - 1; i++)
             {
                 string[] row = data[i].Split(new char[] { ',' });
                 if (row[0] != "")
                 {
                     names q = new names();
                     q.sceneNumber = int.Parse(row[0]);
                     q.sceneYear = int.Parse(row[2]);
                     etc...


 2,Tutorial,1939,9,0,19390900,Tutorial,1000,2000,3000,no
 3,1939 – Wielun,1939,9,1,19390901,1939 – Wielun,2000,3000,5000,no
 4,1939 – Piotrkow,1939,9,2,19390902,1939 – Piotrkow,2000,3000,5000,no

etc...

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by alexianphilosophy · Feb 04 at 05:46 PM

I think I see what you're trying to do with splitting by \n. I recently was working on something with a .tsv source and instead of splitting using text.Split, I used this https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-6.0 File.ReadAllLines() function to split my file's lines. It worked really well for me as I could then iterate through every line and split the line into an array of strings using the Split() function. Maybe try:

     Debug.LogWarning($"Processing catalogue.csv...");

     string[] lines = File.ReadAllLines($"{Application.dataPath}{csvPath}");

     for (int l = 0; l < lines.Length; l++)
     {
         string[] splitData = lines[l].Split(',');
         //Do something with the split data
     }
Comment
Add comment · Show 6 · 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 alexianphilosophy · Feb 04 at 05:47 PM 0
Share

Of course you have to input the csvPath, but it's just a file name. (e.g. catalogue.csv).

avatar image alexianphilosophy · Feb 04 at 05:49 PM 0
Share

Make sure you're using System.IO as well.

avatar image mjonasz · Feb 04 at 09:24 PM 0
Share

I get an error message (feature interpolated strings not avail on C# 4) on the brackets part of the line: string[] data = File.ReadAllLines($"{Application.dataPath}{scenarioInfoTextFile.csv}");

avatar image mjonasz · Feb 04 at 09:26 PM 0
Share

I am not sure why my original code is not working. I use the same exact code in other places and there are no issues there. This one too worked until the other day and I have no idea what I had done that created this problem and can't find a way to debug it. Actually - I did make a change to the CSV by adding a number of lines to it. But I don't see how this could have impacted the code so now it only reads one line.

avatar image alexianphilosophy mjonasz · Feb 05 at 12:56 AM 0
Share

I mean, what's the difference between this and the other locations in which you use the code? You should also try to just use the code snippet I pasted in.

avatar image mjonasz alexianphilosophy · Feb 23 at 06:08 PM 0
Share

No idea what the difference is - I just copied the code from script to script once I made it work the first time around. I tried to use your code snipped but could not make it work.

Follow this Question

Answers Answers and Comments

135 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 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 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 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 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

ReadLine to a Variable 2 Answers

Add asset to array during runtime 0 Answers

csv to unity 0 Answers

Using OpenFilePanel at runtime to open a file as a TextAsset to read data is giving NullReferenceException 1 Answer

Using an Excel generated .csv file 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