C# 将未知行/列数的CSV读取到Unity数组中
我想要一个由CSV文件生成的二维数组,该文件的行/列数未知。列计数是基于标题数据固定的。我需要能够将其处理为一个网格,既跨行又跨列,因此需要数组 目前,我可以将数据拆分为行,然后将每行拆分为组件。然后我将每一行添加到一个列表中。这一切似乎都很好 我不能做的是将字符串数组列表转换为二维数组C# 将未知行/列数的CSV读取到Unity数组中,c#,unity3d,C#,Unity3d,我想要一个由CSV文件生成的二维数组,该文件的行/列数未知。列计数是基于标题数据固定的。我需要能够将其处理为一个网格,既跨行又跨列,因此需要数组 目前,我可以将数据拆分为行,然后将每行拆分为组件。然后我将每一行添加到一个列表中。这一切似乎都很好 我不能做的是将字符串数组列表转换为二维数组 static public string[,] SplitCsvGrid(string csvText) { string[] lines = csvText.Split("\n"[0]);
static public string[,] SplitCsvGrid(string csvText)
{
string[] lines = csvText.Split("\n"[0]);
// finds the max width of row
int width = 0;
for (int i = 0; i < lines.Length; i++)
{
string[] row = SplitCsvLine(lines[i]);
width = Mathf.Max(width, row.Length);
}
// creates new 2D string grid to output to
string[,] outputGrid = new string[width + 1, lines.Length + 1];
for (int y = 0; y < lines.Length; y++)
{
string[] row = SplitCsvLine(lines[y]);
for (int x = 0; x < row.Length; x++)
{
outputGrid[x, y] = row[x];
// This line was to replace "" with " in my output.
// Include or edit it as you wish.
outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\"");
}
}
return outputGrid;
}
它当前在字符串[,]newCSV=csvFile.ToArray行失败;with error无法将类型“string[][]”隐式转换为“string[*,*]”,因此我显然没有正确声明某些内容-我完全不知道是什么
List<string[]> csvFile = new List<string[]>();
void Start()
{
// TODO: file picker
TextAsset sourceData = Resources.Load<TextAsset>("CSVData");
if (sourceData != null)
{
// Each piece of data in a CSV has a newline at the end
// Split the base data into an array each time the newline char is found
string[] data = sourceData.text.Split(new char[] {'\n'} );
for (int i = 0; i < data.Length; i ++)
{
string[] row = data[i].Split(new char[] {','} );
Debug.Log(row[0] + " " + row[1]);
csvFile.Add(row);
}
string[,] newCSV = csvFile.ToArray();
} else {
Debug.Log("Can't open source file");
}
因为您的数据是以表格的形式存在的,所以我强烈建议您使用一个数组,而不是像您当前使用的那样使用2d数组来建模/保存csv中的数据 此数据结构附带了大量预烘焙功能,可使您更轻松地处理数据 如果您采用这种方法,那么还可以使用它将CSV数据复制到数据表中,使用CSV数据的结构来创建数据表 它很容易配置和使用
这只是一个小提示,只要可能,您应该始终尝试使用最适合您的任务的数据结构。想想你用来建造房子的数据结构和算法,你当然可以用螺丝刀敲打钉子,但用锤子更容易、更有效 由于您的数据是以表格的形式存在的,我强烈建议您使用一个数组,而不是像您当前使用的那样使用2d数组来建模/保存csv中的数据 此数据结构附带了大量预烘焙功能,可使您更轻松地处理数据 如果您采用这种方法,那么还可以使用它将CSV数据复制到数据表中,使用CSV数据的结构来创建数据表 它很容易配置和使用
这只是一个小提示,只要可能,您应该始终尝试使用最适合您的任务的数据结构。想想你用来建造房子的数据结构和算法,你当然可以用螺丝刀敲打钉子,但用锤子更容易、更有效 您可以使用此函数获取2d数组
static public string[,] SplitCsvGrid(string csvText)
{
string[] lines = csvText.Split("\n"[0]);
// finds the max width of row
int width = 0;
for (int i = 0; i < lines.Length; i++)
{
string[] row = SplitCsvLine(lines[i]);
width = Mathf.Max(width, row.Length);
}
// creates new 2D string grid to output to
string[,] outputGrid = new string[width + 1, lines.Length + 1];
for (int y = 0; y < lines.Length; y++)
{
string[] row = SplitCsvLine(lines[y]);
for (int x = 0; x < row.Length; x++)
{
outputGrid[x, y] = row[x];
// This line was to replace "" with " in my output.
// Include or edit it as you wish.
outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\"");
}
}
return outputGrid;
}
可以使用此函数获取二维阵列
static public string[,] SplitCsvGrid(string csvText)
{
string[] lines = csvText.Split("\n"[0]);
// finds the max width of row
int width = 0;
for (int i = 0; i < lines.Length; i++)
{
string[] row = SplitCsvLine(lines[i]);
width = Mathf.Max(width, row.Length);
}
// creates new 2D string grid to output to
string[,] outputGrid = new string[width + 1, lines.Length + 1];
for (int y = 0; y < lines.Length; y++)
{
string[] row = SplitCsvLine(lines[y]);
for (int x = 0; x < row.Length; x++)
{
outputGrid[x, y] = row[x];
// This line was to replace "" with " in my output.
// Include or edit it as you wish.
outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\"");
}
}
return outputGrid;
}
List.ToArray生成字符串[],而不是字符串[,]。您确定正确声明了变量类型吗?在C-n-Dimensional(例如2D)中有两种类型的数组,其中为每个维度分配的空间是固定的,并且是锯齿状的,这是一个数组数组,其中每行可以包含可变数量的列。您在此处混合了这两个元素。List.ToArray将生成字符串[],而不是字符串[],]。您确定正确声明了变量类型吗?在C-n-Dimensional(例如2D)中有两种类型的数组,其中为每个维度分配的空间是固定的,并且是锯齿状的,这是一个数组数组,其中每行可以包含可变数量的列。你把这两者混在一起了,谢谢。我不知道这个数据类型存在。我会读更多关于它的内容,因为我认为它对未来的项目很有用。真高兴我能帮上忙!谢谢我不知道这个数据类型存在。我会读更多关于它的内容,因为我认为它对未来的项目很有用。真高兴我能帮上忙!我接受了这个,因为它基本上修复了我的代码。然而,关于datatables的另一个答案非常有用,我接受了这个答案,因为它基本上修复了我的代码。然而,关于数据表的另一个答案也非常有用