1. Home
  2. Data & files

Data files · spreadsheets

Reading Spreadsheet Files into a Matrix: Ranges and Gaps

Eigenfield editorial team ·

A spreadsheet is a grid of cells, and a matrix is a grid of numbers, so reading one into the other sounds like a direct copy. It rarely is. Sheets mix text and numbers, leave cells empty, carry header rows, merge cells, and store dates as serial counts. A reliable import decides what to do about each of these before any value is moved. The advice below applies whichever spreadsheet program produced the file, and it fits alongside the other tasks in Data and files.

Pick the rectangle first

A matrix has one type and a fixed shape, so the first decision is which rectangular block of cells holds the data. Name the range by its corners, for example B2 to F101, which gives 5 columns and 100 rows. The size follows from simple counting: columns are the difference of the column indices plus 1, and rows are the difference of the row numbers plus 1. Here F is the 6th letter and B the 2nd, so 6 − 2 + 1 = 5 columns, and 101 − 2 + 1 = 100 rows.

Keep header text outside the rectangle and store it separately as a list of column names. Mixing a header row into a numeric block either fails the import or converts the whole block to strings. If the data position changes from file to file, search for the first row whose cells are all numeric instead of hard-coding row 2.

Exporting to a plain format

Reading a proprietary workbook directly needs a library that knows the file format. The most portable alternative is to export the sheet as comma-separated or tab-separated text and read that. Text export loses formulas and formatting, which is usually exactly what a numerical script wants: the displayed or stored values and nothing else. Check one thing before relying on it, though. Some programs export the displayed, rounded number rather than the full stored precision, so a cell holding 0.123456789 may reach the file as 0.12. Set the cell format to show enough digits before exporting.

import numpy as np
raw = np.genfromtxt("sheet.csv", delimiter=",", skip_header=1,
                    usecols=range(1, 6), max_rows=100)
print(raw.shape)    # (100, 5)

Blanks, text and dates

An empty cell has no numeric value. Map it to NaN, not to zero, and carry that NaN through the computation deliberately. Statistical routines then either skip the entry or propagate it, but never silently count it as a measurement of zero. Cells containing text where a number is expected, such as n/a or a unit suffix like 12 kg, should be reported with their row and column, then cleaned in the source or parsed with an explicit rule. The parsing itself is covered under ASCII to double conversion.

Dates need special care. Most spreadsheets store a date as a count of days from an epoch, and the epoch differs between programs. A value such as 45000 means nothing until the epoch is known. Convert dates to a standard form while the file is still being read, and do not leave them as bare numbers in the matrix.

Verifying the import

Never trust an import by eye. Compare three things with the sheet itself: the shape of the matrix, the sum of one or two columns, and the count of blank cells. The spreadsheet’s own status display shows sums and counts for a selected range, and a mismatch of even one cell points to a bad range or a text value that was skipped. For a larger check, write the matrix back to a text file and compare it with the original using the methods in comparing two files.

Fields wrapped in quotes are common in exports and have their own rules, described under quoting strings. When the same data is kept in a database instead of a file, connecting to an SQL database gives a more controlled route, and matrix terminology appears in the glossary.

Back to top