If you look at the InputForm of the List you get from Import, you will notice that all those "empty" entries are actually an empty string, i.e. ""
. You can then replace the string with an empty Sequence[], and the empty sequence will automatically be flattened out of the list.
I generated a .CSV file with your pattern of pairs of columns of different lengths, then imported it
data = Import["sample.csv"]
(*Out:
{{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},
{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},
{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},
{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,4,4},{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},
{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},
{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},
{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},{1,1,2,2,3,3,,},
{1,1,,,3,3,,},{1,1,,,3,3,,},{1,1,,,3,3,,},{1,1,,,3,3,,},{1,1,,,3,3,,},
{1,1,,,3,3,,},{1,1,,,3,3,,},{1,1,,,,,,},{1,1,,,,,,},{1,1,,,,,,},
{1,1,,,,,,},{1,1,,,,,,},{1,1,,,,,,},{1,1,,,,,,},{1,1,,,,,,},{1,1,,,,,,}}
*)
If you check the InputForm
for that expression, you will see a lot of the following:
... List[1, 1, 2, 2, 3, 3, "", ""], List[1, 1, 2, 2, 3, 3, "", ""], ...
We can then do the replacement:
cleaneddata = data /. "" -> Sequence[];
cleaneddata // TableForm
(*
{{1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4},
{1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4},
{1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4},
{1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4},
{1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3, 4, 4}, {1, 1, 2, 2, 3, 3},
{1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3},
{1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3},
{1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3},
{1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3}, {1, 1, 2, 2, 3, 3},
{1, 1, 2, 2, 3, 3}, {1, 1, 3, 3}, {1, 1, 3, 3}, {1, 1, 3, 3},
{1, 1, 3, 3}, {1, 1, 3, 3}, {1, 1, 3, 3}, {1, 1, 3, 3}, {1, 1},
{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}
*)
You should then be able to extract pairs of columns from this data set using [[ ]]
, etc.
DeleteCases[imp,{,}]
or using a rule replacementimp /. {,} -> Sequence[]
$\endgroup$""
, in which case the replacement could be even easier. Let me make a toy CSV file to play with... $\endgroup$