Back to Coding

How Do You Work With Spreadsheets and CSV Files?

How Do You Work With Spreadsheets and CSV Files?
AI Tutorials Team 19 June 2026

Most people who learn to script as adults do it because of a spreadsheet. The task is nearly always the same: take a regular export, do something mechanical to it, and produce a tidier version. It is a good first project and it has a specific set of traps.

What is the difference between a CSV and a spreadsheet?

A CSV is a plain text file where values are separated by commas. A spreadsheet file is a compressed archive containing formatting, formulas, multiple sheets and everything else, and it cannot be read as text.

They need different handling, and confusing them is the first thing that goes wrong.

Which should you work with?

CSV, wherever possible. It is simpler, faster and less likely to surprise you. Exporting a spreadsheet to CSV before processing removes a whole category of difficulty, at the cost of losing formatting you probably did not need.

What goes wrong with CSV files?

  • Commas inside values. An address containing a comma splits into two columns unless quoted properly.
  • Encoding. Accented characters arriving as nonsense, which is a character set mismatch.
  • Header rows. Present, absent, or three rows of preamble before the real headers.
  • Line endings. Differing between systems, occasionally producing blank rows.
  • Leading zeros. Lost when something is treated as a number.

Which of those causes the most trouble?

Encoding, because it produces output that is subtly wrong rather than failing. A file exported from one system and read with the wrong character set assumption gives you names with strange symbols in them, and nothing reports an error.

What goes wrong with spreadsheet files?

Merged cells, which have no equivalent in a plain table. Multiple sheets where the script reads the first. Formulas, where the script reads the formula rather than the result depending on how it opens the file. And layouts designed for printing rather than for data, with titles and blank rows above the actual table.

What should you ask for?

The file is a CSV exported from [system], with headers on row one and about four hundred rows. Read it, do [task], and write the results to a new file. Do not modify the original. Print the number of rows read, the number processed and the number skipped, with the reason for any skips.

Why does the skip count matter so much?

Because silent skipping is the characteristic failure of data scripts. A row with an unexpected value gets passed over, the script reports success, and the output is short by eleven rows that nobody notices.

Comparing rows in against rows out, every time, is the single most useful check in this area.

What about dates?

The other reliable source of pain. Dates arrive as text in inconsistent formats, and the ambiguity between day-first and month-first ordering silently produces wrong results for any day below thirteen.

Stating the format explicitly, rather than letting anything guess, is worth the extra line.

Is a script better than a spreadsheet formula?

Not always. If the task can be done with a formula or a built-in feature, that is usually quicker and easier to check. Scripts win when the task repeats, involves several files, or is too fiddly to express in a formula, which is a narrower set than enthusiasm suggests.