# Organising and examining a complicated CSV file

I am being sent data in CSV files. The sender is providing helpful comments and lots of headings so that it is fine to look at in a spreadsheet environment. However, when I import it into Mathematica it becomes difficult to read using TableForm mostly because many of the cells contain comments that are very long.

How do I restrict the length of TableForm entries?

Here is a minimum working example of the results of using Import[] on the CSV file.

ip = {{{"", "", ""}, {"", "", ""}, {"",
"Data for tests on the 14th April 2021", ""}, {"", "", ""}, {"",
"Here is a very long line. Only the start of the line needs to \
be seen since an overview of the spreadsheet is required. ", ""}, {"",
"", ""}, {"",
"The data is in 4 four sheets this sheet is the summary",
""}, {"", "", ""}, {"", "Start ", "End"}, {"Data 1", 2.,
5.}, {"Data 2", 5., 9.}, {"Data 3", 20., 25.}, {"Data 4",
12., 17.}}, {{"x vals", "y vals"}, {2.,
0.3330287479237922}, {3., 0.19273318644865778}, {4.,
0.2689069410951601}, {5., 0.08207429250157161}}, {{"x vals",
"y vals"}, {5., 0.45418961829920845}, {6.,
0.571585992020961}, {7., 0.2085062721975699}, {8.,
0.4228854382612791}, {9., 0.8204853099017002}}, {{"x vals",
"y vals"}, {20., 0.004555883050753273}, {21.,
0.9461665188994238}, {22., 0.5266492863708679}, {23.,
0.993247994762222}, {24., 0.40219540885589733}, {25.,
0.7634085960638198}}, {{"x vals", "y vals"}, {12.,
0.15418438184183136}, {13., 0.6223264477185402}, {14.,
0.9009276274909394}, {15., 0.6832380927198871}, {16.,
0.16021536248120283}, {17., 0.27399014026061996}}};
Dimensions[ip]


The Dimensions[] tells me that there are 5 sheets. So I look at the first sheet using TableForm.

TableForm[ip[[1]], TableSpacing -> {0, 1}]


As you can see the long comments in some CSV cells make some entries in the table much longer than others. I just want an overview of the contents so I don't want to see all the details in each entry. I probably need to do some form of converting to strings and then limiting the number of characters in each string. I am sure there are other possibilities as well. Thanks for helping.

Maybe something like this? The following function will divide the available screen width into equally sized columns and then uses scrollbars to display content when it becomes too tall:

readableGrid[content_List, maxRowHeight : _ : 50, gridOpts : OptionsPattern[]] := Grid[
Map[
Pane[#, ImageSize -> {Full, UpTo[maxRowHeight]},
Scrollbars -> {False, Automatic}, AppearanceElements -> None] &,
content,
{2}
],
gridOpts,
ItemSize -> Scaled[0.99/Max[Length /@ content]],
Alignment -> Left
]


Alternatively, you can use ImageSize -> {UpTo[200], UpTo[maxRowHeight]} (or however wide you want your columns to be at most) in the Pane and leave out the ItemSize option in Grid. You can similarly also put a Pane with a scrollbar around the whole Grid to make sure the output cell itself remains manageable in size.

• Thanks Sjoerd lots to learn from in your post. I was hoping for something more simple like an option to TableForm but as you have shown these things are never so straightforward.
– Hugh
Apr 16, 2021 at 8:46

Using Grid and selecting text to display

grid[data_] := Grid[data, Background -> {None, 1 -> LightYellow}, Dividers -> Center]

heading = (ip[[1, 1 ;; 8]] // Map[Select[# != "" &]]) //. {} -> Nothing // Flatten // First

{grid@ip[[1, 9 ;;]], Splice@Table[grid[ip[[i]]], {i, 2, Length@ip}]}},
Alignment -> Top,
Frame -> All,
ItemStyle -> {Automatic, 1 -> {Bold}}]


Could add a header to each of the sub-grids, "Summary", "Sheet 1", etc.

• Thanks Rohit. Dropping rows from the data is one solution.
– Hugh
Apr 16, 2021 at 8:48