Removing Empty Values โ€‹โ€‹in a Mathematica Table

I import data from some .asc files into Mathematica using the Import[filename, "Data"] command and save it in a table. I ran into a problem when sometimes there are several empty lines at the end of the file in the .asc files, which leads to empty values โ€‹โ€‹in the table, which subsequently lead to some problems.

For example, when I look at data[[5 ;; (Length[data])]] data[[5 ;; (Length[data])]] , I get:

{{3446.05, 15.5156}, {3446.18, 14.5156}, ..., {3451.49, 7.51563}, {}, {}, {}, {}}

So my question is: what is the best way to get rid of these empty values? I looked at either ignoring the spaces in the Import, but did not find anything that could do this. I also looked at Delete , but I cannot get an expression that matches empty values.

One way to do this is to change the Length[data] to 'Length [data] -4`. However, this should potentially be changed for each file, and I would prefer something more generalized solution that will work for any file, regardless of whether they have a space or not.

+4
source share
2 answers

If your imported list was called s , you can use:

 s/.{}->Sequence[] Select[s,Length[#]==2&] DeleteCases[s,{}] Partition[Flatten[s],2] 
+1
source

The easiest way is to use the built-in DeleteCases function:

 data={{1,2},{},{3,4},{}} DeleteCases[data,{}]={{1,2},{3,4}} 
0
source

Source: https://habr.com/ru/post/1498500/


All Articles