How to Convert a TXT File to CSV

If your .txt file is already delimited (tabs, pipes, semicolons), converting it to CSV means replacing the delimiter with commas and saving with a .csv extension. Excel's Text-to-Columns wizard handles this without code.

Generate matching mock data right now

No sign-up, runs 100% in your browser, exports straight to CSV.

Excel: Text-to-Columns

  1. Open Excel, Data → From Text/CSV, select your .txt file.
  1. Excel previews the data and guesses the delimiter — fix it in the dropdown if needed.
  1. Click Load. The data lands in a worksheet.
  1. File → Save As → CSV UTF-8 (Comma delimited).

Python one-liner

import pandas as pd
pd.read_csv('input.txt', sep='\t').to_csv('output.csv', index=False)

Change sep to '|' for pipe-delimited, ';' for semicolon, or r'\s+' for whitespace.

Command line: sed

Tab-delimited to comma-delimited:

sed 's/\t/,/g' input.txt > output.csv

Frequently asked questions

What if my TXT file has irregular columns?

It's not a delimited file — it's prose or a log. Parse it with a regex first to extract fields, then write a CSV.

How do I keep leading zeros?

Import via Excel's Power Query (Data → From Text/CSV) and set the column type to Text before loading.

Related guides

Start generating free mock CSVs

High-fidelity mock tables for tests, demos, or models. No sign-ups, completely local.