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.
Excel: Text-to-Columns
- Open Excel, Data → From Text/CSV, select your .txt file.
- Excel previews the data and guesses the delimiter — fix it in the dropdown if needed.
- Click Load. The data lands in a worksheet.
- 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.