[英]How to create a 2d array from a CSV file
I'm having trouble creating the 2-d array "smartdata" from my CSV file data "smart eye data.csv". 我从CSV文件数据“ smart eye data.csv”创建二维数组“ smartdata”时遇到问题。 I keep getting errors stating "Object reference not set to an instance of an object".
我不断收到错误消息,指出“对象引用未设置为对象的实例”。
I know that 2 for loops will be necessary to create the outer and inner dimensions of the matrix, but still haven't got this to work. 我知道需要2个for循环来创建矩阵的外部和内部尺寸,但仍然没有做到这一点。 The CSV data is just a spreadsheet of numbers.
CSV数据只是数字电子表格。 Any help would be greatly appreciated.
任何帮助将不胜感激。 Thanks
谢谢
using (StreamReader oStreamReader = new StreamReader(File.OpenRead("Smart Eye data.csv")))
{
sFileContents = oStreamReader.ReadToEnd();
}
string[][] smartdata = new string[1000][];
string[] sFileLines = sFileContents.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
int i = 0;
foreach(string sFileline in sFileLines)
{
string[] rowarray = sFileline.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < rowarray.Length; j++)
{
smartdata[i][j] =rowarray[j]; //where the error occurs
//Debug.Log(smartdata[i][j]);
}
i = i + 1 ;
}
if you insist on using a 2d array (I would not) you simply need (and insist on not using csvhelper) 如果您坚持使用二维数组(我不会),您只需要(并坚持不使用csvhelper)
foreach(string sFileline in sFileLines)
{
smartdata[i] = sFileline.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
}
if you want to do it the hard way then do this 如果您想用困难的方式做到这一点,那就这样做
for (int j = 0; j < rowarray.Length; j++)
{
smartdata[i] = new string[rowarray.Length];
smartdata[i][j] =rowarray[j]; //where the error occurs
//Debug.Log(smartdata[i][j]);
}
i = i + 1 ;
Now you can see what my original comment meant. 现在您可以看到我的原始评论的意思。 I a jagged array(what you have ) you have to allocate each row.
我有一个锯齿状的数组(你有什么),你必须分配每一行。
pm100 gave you the real problem: you're not allocating your inner array; pm100给您带来了真正的问题:您没有分配内部数组; hence the error.
因此错误。
Using a CSV library isn't a bad idea - but it certainly isn't a necessity. 使用CSV库并不是一个坏主意-但这当然不是必需的。
Using lists (instead of arrays) has the benefit that you don't need to know the #/rows or #/columns in advance. 使用列表(而不是数组)的好处是您不需要事先知道#/行或#/列。
Here's an example: 这是一个例子:
List<List<string>> mylist = new List<List<string>>();
using (StreamReader sr = new StreamReader(File.OpenRead("Smart Eye data.csv")))
{
string line;
while((line = sr.ReadLine()) != null)
{
System.Console.WriteLine(line);
List<string>row = line.Split(",").ToList();
mylist.Add(row);
}
}
...
You should initialize child array of 2d array: 您应该初始化2d数组的子数组:
foreach(string sFileline in sFileLines)
{
string[] rowarray = sFileline.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
smartdata[i]=new string[rowarray.Length];
for (int j = 0; j < rowarray.Length; j++)
{
smartdata[i][j] =rowarray[j]; //where the error occurs
//Debug.Log(smartdata[i][j]);
}
i = i + 1 ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.