- Home /
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);
}
}
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
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
Follow this Question
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