var [][] 数组删除特定单词

var [][] array remove specific words

我遇到了一个小问题。我有一个带有 "NaN" 值和双精度值(例如 0.6034)的 .csv,我试图将 CSV 的双精度值读入数组 [y][x]。

目前,我阅读了整个 .csv,但之后无法删除所有 "NaN" 值。 (它应该通过 CSV 解析并将数字添加到数组 [y][x] 并保留所有 "NaN")

我当前的代码:

 var rows = File.ReadAllLines(filepath).Select(l => l.Split(';').ToArray()).ToArray(); //reads WHOLE .CSV to array[][]


        int max_Rows = 0, j, rank;
        int max_Col = 0;
        foreach (Array anArray in rows)
        {
            rank = anArray.Rank;
            if (rank > 1)
            {
                 // show the lengths of each dimension
                for (j = 0; j < rank; j++)
                {

                }
            }
            else
            {

            }
            // show the total length of the entire array or all dimensions

            max_Col = anArray.Length; //displays columns
            max_Rows++;  //displays rows
        }

我尝试了搜索,但找不到任何对我有帮助的东西。 我知道这可能真的很简单,但我是 C# 的新手。

.CSV 和期望的结果:

NaN;NaN;NaN;NaN
NaN;1;5;NaN
NaN;2;6;NaN
NaN;3;7;NaN
NaN;4;8;NaN
NaN;NaN;NaN;NaN

这是我拥有的示例 .csv。我应该更清楚,对不起!每行都有一个 NaN。我希望它像这样显示:

1;5
2;6
3;7
4;8

这只是 .csv 的一个示例,真正的 csv 有大约 60.000 个值...我需要使用 [y][x] 获取输入,例如 [0][0] 应该显示“1” [2][1] 应该显示“7”等等。

再次感谢您的帮助!

您可以过滤数组中的分隔值。

我稍微修改了你的代码。

 File.ReadAllLines(filepath).Select(l => l.Split(';').ToArray().Where(y => y != "NaN").ToArray()).ToArray();

如果您想删除所有包含 NAN 的行(CSV 的典型任务 - 清除所有 不完整的行 ),例如

  123.0; 456; 789
    2.1; NAN;  35     <- this line should be removed (has NaN value)
     -5;   3;  18

你可以这样实现

  double[][] data = File
    .ReadLines(filepath)
    .Select(line => line.Split(new char[] {';', '\t'},
                               StringSplitOptions.RemoveEmptyEntries))
    .Where(items => items  // Filter first...
       .All(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase)))
    .Select(items => items
       .Select(item => double.Parse(item, CultureInfo.InvariantCulture))
       .ToArray()) // ... materialize at the very end
    .ToArray();

使用string.Join显示行数:

 string report = string.Join(Environment.NewLine, data
   .Select(line => string.Join(";", line)));

 Console.Write(report);

编辑: 实际问题是仅从 CSV 中获取第 2 和第 3 个完整列:

NaN;NaN;NaN;NaN
NaN;1;5;NaN
NaN;2;6;NaN
NaN;3;7;NaN
NaN;4;8;NaN
NaN;NaN;NaN;NaN

期望的结果是

[[1, 5], [2, 6], [3, 7], [4, 8]]

实现:

double[][] data = File
  .ReadLines(filepath)
  .Select(line => line
     .Split(new char[] {';'},
            StringSplitOptions.RemoveEmptyEntries)
     .Skip(1) 
     .Take(2)
     .Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase))
     .ToArray())
  .Where(items => items.Length == 2)
  .Select(items => items
    .Select(item => double.Parse(item, CultureInfo.InvariantCulture))
    .ToArray())
  .ToArray();

测试

// 1
Console.Write(data[0][0]);
// 5
Console.Write(data[0][1]);
// 2
Console.Write(data[1][0]);

一次性所有值:

string report = string.Join(Environment.NewLine, data
   .Select(line => string.Join(";", line)));

Console.Write(report);

结果:

1;5
2;6
3;7
4;8 

编辑 2:如果您只想提取非 NaN 值(请注意,初始 CSV 结构将被 破坏) :

1;2;3              1;2;3
NAN;4;5            4;5   <- please, notice that the structure is lost
6;NAN;7        ->  6;7
8;9;NAN;           8;9
NAN;10;NAN         10
NAN;NAN;11         11 

然后

double[][] data = File
  .ReadLines(filepath)
  .Select(line => line
     .Split(new char[] {';'},
            StringSplitOptions.RemoveEmptyEntries)
     .Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase)))
  .Where(items => items.Any()) 
  .Select(items => items
    .Select(item => double.Parse(item, CultureInfo.InvariantCulture))
    .ToArray())
  .ToArray();