Scrapeless Wiki

How to Export Scraped Data to CSV – Practice and Pitfalls

Informational P3 how to export scraped data to csv

Learn how to write CSV that survives spreadsheets: encoding, quoting, identifiers, dates, and the conventions that prevent silent corruption.

CSV is the format people ask for, because it opens in a spreadsheet. Writing it well means anticipating what the spreadsheet will do to your data on the way in.

1. Use a Library

import csv

with open("out.csv", "w", newline="", encoding="utf-8") as handle:
    writer = csv.DictWriter(handle, fieldnames=COLUMNS, extrasaction="ignore")
    writer.writeheader()
    writer.writerows(records)

newline="" prevents doubled line endings on some platforms, and the library handles quoting and escaping correctly. String-joining fields is where delimiter bugs come from.

2. Quote Anything Ambiguous

A value containing the delimiter, a quote, or a line break must be quoted, with embedded quotes doubled. Libraries do this; quoting conservatively beyond the minimum is harmless and safer.

Better still, strip literal newlines from values on the way out β€” many consumers handle quoted newlines badly even though the format permits them.

3. Protect Identifiers From Spreadsheets

This is the failure that silently corrupts data. A spreadsheet reading 00123 shows 123, and reading a long numeric code shows scientific notation.

The value in the file is correct; the spreadsheet reinterprets it. If the recipient will open it in a spreadsheet and the identifier matters, the practical protections are to keep identifiers in a format the spreadsheet will not coerce, or to state in the accompanying note that the column must be imported as text.

4. Dates in ISO Form

Write 2026-08-18 or a full ISO timestamp. No locale reinterprets it, and it sorts correctly as text.

Locale-formatted dates are the second most common corruption: 03/04/2026 is April in one place and March in another, and a spreadsheet will convert it according to its own settings.

5. Declare the Encoding Out of Band

CSV carries no encoding declaration. Write UTF-8 and tell the recipient.

A byte order mark helps some spreadsheet versions detect UTF-8 and breaks strict parsers, so it is a choice to make deliberately based on who consumes the file β€” not a default.

6. Be Explicit About Nulls

CSV cannot distinguish empty from null. Choose one representation, document it, and apply it consistently.

An empty field is the usual convention. Writing the literal text None or NULL produces a string that looks like data, which is worse than ambiguity.

7. Include Context

A CSV of values without context invites wrong conclusions. Add columns for the source URL and the collection timestamp, and supply a short note with the file stating the schema, the null convention, the encoding, and what the data does not cover.

8. Summary

Write CSV with a library, quote conservatively, use ISO dates, choose and document a null representation, and write UTF-8 while telling the recipient.

The corruption that matters happens after the file leaves you: spreadsheets coerce identifiers and dates. Anticipate that, and ship a note describing the schema and the collection context alongside the data.