How to Convert Text to CSV: Complete Guide
Multiple methods to convert plain text, lists, and tabular data into CSV format.
Converting text data to CSV format is a common task for data analysts, developers, and anyone who works with spreadsheets. This guide covers multiple methods, from the quickest online tools to programmatic solutions.
Method 1: Online Text to CSV Converter (Fastest)
The quickest way to convert text to CSV is using an online converter. Our Text to CSV Converter is free, works directly in your browser, and requires no signup.
How to use:
- Paste your text data into the input box
- Select your input delimiter (tab, space, comma, etc.)
- Choose your output format options
- Click "Convert to CSV"
- Download the file or copy to clipboard
Privacy benefit: All conversion happens in your browser. Your data is never uploaded to any server.
Method 2: Using Microsoft Excel
Excel can import text files and convert them to CSV format.
For Tab or Comma-Separated Text:
- Open Excel and go to File → Open
- Change file type to "All Files" and select your text file
- The Text Import Wizard will open
- Choose "Delimited" and click Next
- Select your delimiter (Tab, Comma, Space, etc.)
- Click Finish to import
- Save as CSV: File → Save As → CSV (Comma delimited)
For Lists (One Item Per Line):
- Copy your list
- Open Excel and paste into column A
- Each line becomes a row
- Save as CSV
Method 3: Using Google Sheets
Google Sheets provides an easy way to convert text to CSV:
- Open Google Sheets and create a new spreadsheet
- Paste your text data into cell A1
- If data is in one column, select it and go to Data → Split text to columns
- Choose your separator (detect automatically or select manually)
- Download as CSV: File → Download → Comma-separated values (.csv)
Method 4: Using Python
For automated or batch conversions, Python is an excellent choice:
import csv
# Read text file
with open('input.txt', 'r') as f:
lines = f.readlines()
# Write CSV file
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
for line in lines:
# Split by tab (change '\t' to your delimiter)
row = line.strip().split('\t')
writer.writerow(row)
print("Conversion complete!")The Python csv module handles proper escaping automatically, ensuring RFC 4180 compliance.
Method 5: Using JavaScript (Node.js)
For web developers or Node.js environments:
const fs = require('fs');
const text = fs.readFileSync('input.txt', 'utf8');
const lines = text.trim().split('\n');
const csv = lines.map(line => {
const fields = line.split('\t');
return fields.map(field => {
// Escape quotes and wrap in quotes if needed
if (field.includes(',') || field.includes('"') || field.includes('\n')) {
return '"' + field.replace(/"/g, '""') + '"';
}
return field;
}).join(',');
}).join('\n');
fs.writeFileSync('output.csv', csv);
console.log('Conversion complete!');Method 6: Command Line (Linux/Mac)
For quick conversions in the terminal:
Tab to CSV (simple):
sed 's/\ /,/g' input.txt > output.csvSpace to CSV (simple):
sed 's/ /,/g' input.txt > output.csvWarning: These simple sed commands don't handle quoting or escaping. If a field contains a comma, the output will be corrupted. For production use, prefer a proper CSV-aware tool.
Safe command-line conversion with proper quoting:
Use Python one-liners for reliable conversions that handle special characters correctly:
# Tab-separated to CSV (with proper quoting)
python3 -c "
import csv, sys
reader = csv.reader(sys.stdin, delimiter='\t')
writer = csv.writer(sys.stdout)
for row in reader:
writer.writerow(row)
" < input.txt > output.csv
# Pipe-separated to CSV
python3 -c "
import csv, sys
reader = csv.reader(sys.stdin, delimiter='|')
writer = csv.writer(sys.stdout)
for row in reader:
writer.writerow(row)
" < input.txt > output.csvThis approach uses Python's csv module to handle quoting, escaping, and RFC 4180 compliance automatically.
Common Input Formats
Tab-Separated (TSV)
Name Email Age
John john@example.com 28
Jane jane@example.com 34Space-Separated
John Smith 28 NYC
Jane Doe 34 LASimple List
Apple
Banana
Orange
GrapePipe-Separated
John|john@example.com|28
Jane|jane@example.com|34Handling Encoding Issues
Character encoding is one of the most common problems when converting text to CSV. If your input text contains non-ASCII characters (accented letters, Chinese/Japanese characters, emojis), you need to handle encoding correctly:
- Always save as UTF-8: UTF-8 supports all characters from every language and is the modern standard.
- Add a BOM for Excel: If the CSV will be opened in Microsoft Excel, add a UTF-8 BOM (byte order mark:
EF BB BF) at the start of the file. Without it, Excel on Windows may misinterpret non-ASCII characters. - Detect source encoding: If you receive a text file with garbled characters, the file was likely saved in a different encoding (e.g., Latin-1, Windows-1252, or Shift-JIS). In Python, the
chardetlibrary can auto-detect encoding.
# Python: Convert text file with unknown encoding to UTF-8 CSV
import chardet
import csv
# Detect encoding
with open('input.txt', 'rb') as f:
detected = chardet.detect(f.read())
encoding = detected['encoding']
# Read with detected encoding, write as UTF-8 CSV
with open('input.txt', 'r', encoding=encoding) as f:
lines = f.readlines()
with open('output.csv', 'w', newline='', encoding='utf-8-sig') as f:
writer = csv.writer(f)
for line in lines:
writer.writerow(line.strip().split('\t'))Batch Conversion: Multiple Files at Once
If you have many text files to convert, here's how to batch-process them:
# Bash: Convert all .txt files in a directory to CSV
for file in *.txt; do
python3 -c "
import csv, sys
reader = csv.reader(sys.stdin, delimiter='\t')
writer = csv.writer(sys.stdout)
for row in reader:
writer.writerow(row)
" < "$file" > "${file%.txt}.csv"
done
echo "Converted $(ls *.csv | wc -l) files"Best Practices
- Check your delimiter: Identify what separates your columns (tab, space, comma, pipe, etc.) before converting. Opening the file in a text editor makes this obvious.
- Handle special characters: Ensure commas and quotes in your data are properly escaped. Never concatenate CSV strings manually — use a library.
- Use UTF-8 encoding: Preserve international characters by using UTF-8. Add a BOM (
utf-8-sigin Python) for Excel compatibility. - Include headers: Add column names in the first row for clarity. This makes the CSV self-documenting and easier to import into databases.
- Validate output: Open the CSV in a spreadsheet to verify correct parsing. Check that columns align properly and no data is split across wrong columns.
- Handle edge cases: Empty fields, fields with only whitespace, and fields with line breaks all need special handling. A proper CSV library handles these automatically.
- Preserve data types: ZIP codes, phone numbers, and account numbers with leading zeros will lose those zeros when opened in Excel. Consider formatting guidance for recipients.
Quick Start
For most users, the fastest solution is our online converter. Just paste your text, click convert, and download. No signup required, and all processing happens in your browser — your data never leaves your computer.
Try Text to CSV Converter