The var [] [] array removes certain words

I got a little problem. I have a .csv with values ​​"NaN" and doubles (for example 0.6034) and I am trying to read only CSV doubles into the [y] [x] array.

I am currently reading the entire .csv, but after that I cannot delete all the "NaN" values. (It should parse the CSV and just add the numbers to the [y] [x] array and leave everything "NaN")

My current code:

 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
        }

      

I tried searching but couldn't find anything that worked for me. I know this is probably very simple, but I am new to C #.

.CSV and desired output:

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

      

This is the .csv sample I have. I should have been clearer, sorry! Each line has NaN. and I want it to display like this:

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

      

This is just a sample CSV. The real csv has arround 60.000 Values ​​... I need to get an input with [y] [x] eg [0] [0] should display "1" and [2] [1] should display "7" etc. .d.

Thanks again for your help!

+3


source to share


2 answers


If you want to delete all lines containing NAN

(a typical CSV task is to clear all uncompleted lines) eg

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

      

You can implement it this way

  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();

      

Use string.Join

to display lines:

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

 Console.Write(report);

      

Edit: The actual problem is to take the 2nd and 3rd full columns from CSV only:

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

      

desired result

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

      

implmentation:



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();

      

Tests

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

      

All values ​​in one go:

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

Console.Write(report);

      

Result:

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

      

Edit 2 : if you only want to extract NaNN values ​​(please note that the original CSV structure will be destroyed ):

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 

      

then

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();

      

+1


source


You can filter your separable values ​​in an array.

I changed your code a bit.



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

      

+6


source







All Articles