Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
0
Question by ratchetunity · Nov 18, 2019 at 10:21 AM · datacsvreading dataexcelchar

CSV Reader delimiter problem

Hi!

I have a script that reads a csv file and generates data in order to instantiate a prefab. I'm currently using ';' as delimiter to separate, but if there is a ";" in the string it jumps into the next column. Any idea on how I can have a ";" in a string and it doesn't go to the next column? This is the code:

 [SerializeField] DataScript dataObject;

 private void Start ()
 {
     CreateDataObjects();
 }
 
 private void CreateDataObjects ()
 {
     StreamReader strReader = new StreamReader(Path.GetFullPath("dataObjectsSheet.csv"));
     strReader.ReadLine();
     bool endOfFile = false;
     while (!endOfFile && (strReader.Peek() != -1))
     {
         string data_String = strReader.ReadLine();
         if (data_String == null)
         {
             endOfFile = true;
             break;
         }
         var dataValues = data_String.Split(';');
         dataObject.name = dataValues[0].ToString();
         dataObject.month = float.Parse(dataValues[1].ToString());
         dataObject.ranking = int.Parse(dataValues[2].ToString());
         dataObject.days = int.Parse(dataValues[3].ToString());
         dataObject.nick = dataValues[4].ToString();
         dataObject.bio = dataValues[5].ToString();
         dataObject.url = dataValues[6].ToString();
         Instantiate(dataObject, transform);
     }
 }
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

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 18, 2019 at 06:53 PM

Well, the classical CSV format would simply put a value in double quotes if i contains a delimiter, double quote or new line character. Of course you can not simply split a line at the seperation character and you have to actually parse through the text, character by character. That's what string.split does anyways internally. Even CSV is a simple format it still has some rules you have to follow. Simply splitting the string at a delimiter does work for trivial cases but it does not work for the actual specified CSV format.


I've quickly created this CSVParser which you can use like this:

 using (StreamReader strReader = new StreamReader(Path.GetFullPath("dataObjectsSheet.csv")))
 {
     CSVParser parser = new CSVParser(strReader, ';');
     var line = new List<string>();
     parser.NextLine(line); // skip header line
     while (parser.NextLine(line))
     {
         // use line[0] to access the first "value", line[1] for the second, ...
     }
 }

Keep in mind that a line in the CSV file could potentially contain different number of values / columns. Blindly reading say line[5] would break if a line doesn't contain at least 6 values. So further parsing the values might need some additional checks.


Also note that float.Parse without a specific culture will use the system culture. So if this code should run on a user's machine, make sure the user uses the right culture or, if needed, use a specific culture. The most common culture is the invariant culture since it won't change in the future. Country specific cultures can change in the future which could break your program. For example my local culture is german so our decimal delimiter is the comma while in the english speaking world the dot is usually used. Note that there are quite a few countries which use the comma

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 ratchetunity · Nov 19, 2019 at 08:52 AM 0
Share

Hi @Bunny83 Thanks for your answer! It seems to work like a charm! I've tried having 5 values (an "aaaaa" string) in line[5] and doesn't seem to break.

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

115 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

Related Questions

Write Data From List To CSV File 0 Answers

Arduino Temperature Sensor Read Values in Unity 5 Answers

Is it bad to use csv/xml resource to read data for mobile 1 Answer

Can I download and then read a CSV all at runtime? If so, how? 1 Answer

Creating a spreadsheet with differents versions of materials preset. 0 Answers


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