Home / Blog / Transliteration & conversion

Spreadsheet formats & CSV

How To Convert Data To CSV In Python?

Use Python’s csv module to serialize rows or dictionaries into CSV.

How To Convert Data To CSV In Python?

Use Python’s csv module to serialize rows or dictionaries into CSV. Choose an explicit field order and let the writer escape commas, quotes and newlines; string concatenation cannot safely handle those cases.

Set up the conversion

For records keyed by column name, DictWriter makes the export schema visible. The example writes id, name and note in that order and rejects unknown keys. The values are an illustrative local fixture, not instructions to collect personal data. Replace the records with your own authorized application data while preserving the schema checks.

A reproducible conversion example

import csv

fields = ["id", "name", "note"]
records = [
    {"id": "00123", "name": "Lee, Ana", "note": 'He said "yes"'},
    {"id": "00456", "name": "Zoë", "note": ""},
]
with open("output.csv", "w", encoding="utf-8", newline="") as target:
    writer = csv.DictWriter(target, fieldnames=fields, extrasaction="raise")
    writer.writeheader()
    writer.writerows(records)
with open("output.csv", encoding="utf-8", newline="") as source:
    assert list(csv.DictReader(source)) == records

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

This fixture exercises leading zeros, a comma, an embedded quote, non-ASCII text and an empty field. Those cases expose the errors that a simple numeric example misses. Confirm the round-trip assertion succeeds in your environment. If rows can have missing keys, decide whether a blank field is acceptable; DictWriter’s default missing-field behavior should not accidentally become your data policy.

Format-specific limits

CSV contains strings on read-back, so convert numeric and date fields under an explicit receiving schema. None becomes an empty field in the standard writer, which loses the distinction from an empty string. A stable field order helps downstream jobs and human review; do not derive it from whichever record happens to arrive first.

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