Home / Blog / Transliteration & conversion

Spreadsheet formats & CSV

How To Convert To CSV In Python?

Choose a reader for the actual source format, then write parsed records with Python’s csv module.

How To Convert To CSV In Python?

Choose a reader for the actual source format, then write parsed records with Python’s csv module. CSV conversion is a mapping from structured values to rows and columns; opening a binary workbook as text or renaming its extension does not perform that mapping.

Set up the conversion

For an existing list of rows, csv.writer is enough. Use DictWriter when records have named fields, pandas or an XLSX library for spreadsheets, and json plus an explicit field mapping for JSON. This example demonstrates the final serialization stage with rows whose field positions are already defined.

A reproducible conversion example

import csv

rows = [
    ["id", "name", "note"],
    ["00123", "Lee, Ana", 'He said "yes"'],
    ["00456", "Zoë", ""],
]
if any(len(row) != 3 for row in rows):
    raise ValueError("Expected exactly three fields per row")
with open("output.csv", "w", newline="", encoding="utf-8") as target:
    csv.writer(target).writerows(rows)
with open("output.csv", newline="", encoding="utf-8") as source:
    assert list(csv.reader(source)) == rows

The code is an illustrative conversion core. Run it in an isolated working folder with the named test files, install only the dependencies used by the example, and handle its reported errors before applying it to a larger dataset. It does not contact a remote conversion service.

Check the expected result

Treat the first row as a header only if the receiving application expects one. Verify column order, row count and exact strings after reading the CSV again. The fixture’s comma and quotation mark must remain inside their original fields. A file that opens without an error can still have the wrong delimiter or shifted columns, so visual appearance alone is insufficient.

Format-specific limits

A source with repeated child records may need several output tables rather than one wide table. A workbook with multiple sheets normally requires one CSV per sheet. Images and PDF layouts need extraction and human verification before serialization. The writer cannot infer missing structure or recover text that was never extracted.

Verify the file contract before using the result

Write down the delimiter, encoding, header names, field order and treatment of empty values. Keep the original file unchanged and write the converted output to a different path. Conversion is successful when a parser for the destination format recovers the intended values; a familiar filename extension or a successful save message is not enough.

Start with a small fixture that contains the cases your real data can contain. Include a leading-zero identifier, a quoted comma, an embedded quotation mark, a blank field and a non-ASCII character. Add a multiline field only if both formats and the receiving system support it. Verify the fixture first, then run a bounded sample from the real input. Record parsed row counts and check the first and last records so truncation or an accidental duplicate header is visible.

Keep identifiers separate from quantities. An account-like or product identifier may contain digits while still being text; converting it to a floating-point number can remove leading zeros or precision. Dates also need an agreed format. When a conversion produces an unexpected blank, investigate whether it came from an empty source cell, a missing field, an invalid value or an automatic missing-value rule. Those conditions can require different downstream handling.

Do not resolve malformed records by silently skipping them unless that behavior is an explicit requirement and the skipped records are reported. Use a failure message that identifies the record or field, retain enough context to correct the input, and avoid exposing sensitive contents in public logs. For production automation, write to a temporary destination and promote it only after validation so a failed run cannot replace a previously good export.

Where the browser tool fits

Convert Text to CSV helps with already extracted plain text. It does not replace a workbook reader, a JSON parser, an XML schema validator or a Parquet engine. Use it only when its input structure and delimiter choices match your actual data. For structured source files, perform the format-aware conversion above and check the result with an independent reader.

Primary reference

Official format and API documentation