Home / Blog / Other text utilities

Text and CSV conversion

Convert CSV to TXT by Choosing the Text You Want to Export

Choose between retaining CSV text and extracting a named column, with a tested export that preserves commas and rejects ambiguous multiline names.

Convert CSV to TXT by Choosing the Text You Want to Export

CSV is already a text format. To convert a CSV file to TXT, decide whether you want the same comma-delimited content with a different extension or a different text layout. A one-name-per-line list, a tab-delimited table and a readable report are different outputs and need different conversion rules.

Choose the output structure

If a receiving system merely requires a .txt filename while still expecting comma-delimited CSV, preserve the existing file content and save a copy under that name. Confirm the delimiter, quoting and encoding requirements with the receiver. The extension change does not alter the records.

If you need one selected column as a plain list, parse the CSV and extract that column by its header. The worked example below exports a column named name. Other columns are intentionally excluded, so retain the original CSV if they will be needed later.

Extract one named column

import csv

with open("input.csv", encoding="utf-8-sig", newline="") as source:
    rows = list(csv.reader(source, strict=True))
if not rows or rows[0].count("name") != 1:
    raise ValueError("Expected exactly one name column")
width = len(rows[0])
if any(len(row) != width for row in rows):
    raise ValueError("Inconsistent field count")
column = rows[0].index("name")
names = [row[column] for row in rows[1:]]
if any("\n" in name or "\r" in name for name in names):
    raise ValueError("A multiline value cannot fit one output line")

with open("names.txt", "w", encoding="utf-8", newline="") as target:
    for name in names:
        target.write(name + "\n")

Run the script beside input.csv. It writes names.txt only after the input structure and selected values have passed the checks. A name such as Rao, Anika becomes one complete output line with its comma intact. The comma is no longer a separator because the output contract is one name per line.

The code requires exactly one column whose header is name, with that spelling and case. It rejects a missing header or duplicate name headers. Guessing which duplicate column was intended would produce an output file that looks usable while potentially containing the wrong values.

Every record must have the same field count as the header. A missing final field and an explicitly empty final field are different structural situations: the former can shorten the record, while the latter occupies its expected position. A CSV parser preserves that distinction better than splitting a line on commas.

Handle embedded line breaks deliberately

A valid CSV field can contain a newline when quoted. The script rejects such a value in the name column because one value could otherwise become several lines in names.txt. That would break the promised one-record-per-line output.

If multiline values are legitimate, choose a format that can represent them, such as CSV with appropriate quoting, JSON strings or another specified escape convention. Replacing the line breaks with spaces is possible only as an explicit content change; it is not a lossless conversion.

Decide what empty values mean

An empty name becomes an empty output line. This preserves its position relative to the other records. If the destination rejects blank lines, decide whether to omit those records, supply a reviewed value or stop the export. Do not quietly remove them when row correspondence matters.

The script retains spaces within and at the edges of each selected value. Trimming, normalizing case or removing duplicates would alter the source strings. Add those steps only when the output specification calls for them and after checking representative records.

Verify both extraction and encoding

Test a comma-containing name, a quoted name, a non-English name and an empty name. Compare the output line order with the source record order. Then change one source name and repeat the export to confirm that the destination reflects the current file.

The example writes UTF-8. If another application displays garbled characters, check how it opens that text instead of assuming the source name is wrong. A different required encoding should be selected deliberately, with errors reported for characters it cannot represent.

Keep the richer source

A names-only TXT file cannot recover the other CSV columns or their header information. Keep the original table and document which column was extracted. When the goal is a full readable report, design and label all needed fields rather than using this intentionally narrow one-column export.

Reference

Python’s CSV reader handles quoted delimiters and multiline fields before the export applies its stricter line-list contract.