Home / Blog / Other text utilities

Text and CSV conversion

Turn a One-Item-Per-Line TXT List into a Single CSV Column

Convert a plain list into one CSV column while keeping commas, quotes, leading spaces and blank records under an explicit line-based contract.

Turn a One-Item-Per-Line TXT List into a Single CSV Column

If a TXT file contains one item per physical line, you can turn it into a one-column CSV by treating each line as a complete field and using a CSV writer. A comma inside an item stays inside that item. The output writer supplies quoting instead of interpreting the comma as another column.

Confirm that a line means one item

This method suits a list of labels, names or identifiers that genuinely uses one line per item. It does not suit a prose document whose lines wrap visually, a table with several fields per line, or a file where one item is allowed to contain an embedded newline.

For example, a source line reading Tea, coffee becomes one quoted CSV field, not two separate products. A line reading 00123 stays an identifier string. A line with leading spaces keeps those spaces because this converter removes only the stored line-ending characters.

Write a one-column table

import csv

with open("input.txt", encoding="utf-8-sig", newline="") as source:
    values = [line.rstrip("\r\n") for line in source]

with open("output.csv", "w", encoding="utf-8", newline="") as target:
    writer = csv.writer(target, lineterminator="\n")
    writer.writerow(["item"])
    writer.writerows([value] for value in values)

The program reads input.txt and writes output.csv with the header item. It accepts UTF-8 text with an optional byte-order mark. Change the header deliberately if the importing system requires a different field name; the filename alone does not define a column schema.

Choose a blank-line policy

The published program preserves blank source lines as empty CSV records containing one field. That keeps the number and order of source items consistent with the stated line-based contract. A blank item may be meaningful, or it may be an accidental empty entry that should be reviewed.

A final newline simply terminates the preceding item. It does not create another item by itself. Two consecutive line endings contain a blank physical line between them, which the program preserves. Test this distinction if exact row counts are important to your import.

If your approved rule is to remove empty items, filter only values equal to an empty string after reading. Filtering with value.strip() would also remove lines containing spaces and would introduce a different rule. Record whichever policy you choose instead of making cleanup an unnoticed side effect.

Let the writer handle punctuation

The writer escapes a quotation mark by doubling it inside a quoted field and quotes fields that need protection. Do not manually add a leading and trailing quote around every line before calling csv.writer, because that would add quote characters to the data itself.

Repeated values remain repeated records. Deduplicating the list, sorting it or changing its case would be additional transformations with their own consequences. If those operations are required, apply and review them separately from the conversion to a table.

Test a small list first

Use four source items: Tea, coffee; 00123; He said "yes"; and a label with two leading spaces. Parse output.csv with a CSV reader and check that it contains the item header followed by those same four strings. Looking only at the raw commas is not a reliable way to count fields.

Add a blank line in a second fixture and confirm that the corresponding output row has one empty field. Also try a file without a final newline; its final item should still appear. These cases catch common errors caused by broad trimming or simplistic string splitting.

Check the receiving application

A spreadsheet can infer that 00123 is a number and display 123 even when the CSV text preserved the zeros. Choose a text column during import when those zeros matter. The CSV format carries field text, not a universal spreadsheet cell-type declaration.

Keep input.txt until the imported rows have been checked. The CSV header and quoting are new structural information, so renaming the output back to .txt does not recreate the original line list. If you need a reverse export, read the CSV and extract the item column under the same newline policy.

Reference

The Python CSV writer performs the field quoting used here. This guide supplies the separate rule that each physical input line is one item.