PrivateCSV

How to convert a TXT file to CSV

If the values in your text file are already separated by commas, renaming it from .txt to .csv is enough. If they are separated by tabs, semicolons, pipes or spaces, import the file in Excel (Data > From Text/CSV) or Google Sheets, choose that delimiter, then save the result as CSV. Here is how to tell which case you have, and how to handle each one, including fixed-width reports.

Last updated

First, look at how the values are separated

Open the .txt in Notepad or TextEdit. A text file that holds a table is usually one of these:

  • Commas between values: it is already a CSV. Just rename it (next section).
  • Tabs between values (the columns line up, with big gaps): a tab-delimited file, common for exports from databases and accounting software.
  • Semicolons or pipes (|) between values: a delimited file with another separator.
  • Columns padded with spaces, often with a dashed line under the header: a fixed-width report.
export.txt (tab-delimited)
name	city	amount
Ana García	Madrid	120.50
Lee Min	Seoul	89.00
report.txt (fixed width)
NAME            CITY        AMOUNT
--------------- ----------- ------
Ana García      Madrid      120.50
Lee Min         Seoul        89.00

Already comma-separated: rename it

  1. Show file extensions: on Windows, File Explorer > View > Show > File name extensions; on a Mac, Finder > Settings > Advanced > Show all filename extensions.
  2. Rename data.txt to data.csv and confirm the change.

Check that values containing a comma are in double quotes, otherwise they will be split. The CSV Validator flags rows with the wrong number of columns, and the CSV Viewer shows the result as a table.

Online, any separator

TXT to CSV detects how the columns are separated (tabs, pipes, semicolons, runs of spaces, fixed-width columns or one value per line), leaves out report titles and ruling lines, and shows the table before you download the CSV. The file is converted in your browser and never uploaded.

In Excel

  1. Open a blank workbook and go to Data > From Text/CSV, then select the .txt file (set the file filter to All files if you do not see it).
  2. In the preview, choose the Delimiter (Tab, Semicolon, Space, or Custom for a pipe) and set File Origin to 65001: Unicode (UTF-8).
  3. Click Load.
  4. Choose File > Save As and pick CSV UTF-8 (Comma delimited).

For a fixed-width report, open the .txt with File > Open instead: the Text Import Wizard starts, choose Fixed width and click in the preview to place the column breaks.

In Google Sheets

  1. Choose File > Import > Upload and select the .txt file.
  2. Set Separator type to Tab, or Custom and type the separator (; or |).
  3. Click Import data, then File > Download > Comma-separated values (.csv).

More on importing in How to open a CSV file in Google Sheets.

With Python

txt_to_csv.py
import pandas as pd

# sep="\t" for tabs, "|" for pipes, r"\s{2,}" for runs of spaces
df = pd.read_csv("report.txt", sep="\t")
df.to_csv("report.csv", index=False, encoding="utf-8")

Choosing the right method

Which TXT to CSV method fits your file
Your fileBest method
Commas, values with commas quotedRename to .csv
Tabs, semicolons or pipesExcel import, Google Sheets or TXT to CSV
Fixed-width reportExcel Text Import Wizard or TXT to CSV
One value per line (a list)TXT to CSV, or Column to comma-separated list
Dozens of filesA Python script

Need a single line of comma-separated values rather than a table? Use Column to comma-separated list. To go back from CSV to text, use CSV to TXT.

Frequently asked questions

Can I just rename a .txt file to .csv?

Yes, if the values are already separated by commas and values containing commas are in double quotes. Turn on file extensions (File Explorer > View > Show > File name extensions on Windows), then rename data.txt to data.csv. If the file uses tabs or spaces, renaming is not enough.

How do I convert Notepad text to CSV?

In Notepad, write one row per line with commas between values and choose File > Save As, Save as type All files, a name ending in .csv and Encoding UTF-8. For an existing text file with tabs or spaces, import it in Excel or use a TXT to CSV converter.

Can I replace the tabs with commas in a text editor?

It works only if no value contains a comma. Otherwise those values get split into two columns. Importing the file, or using a converter that adds quotes where needed, is safer.

How do I convert a fixed-width text report to CSV?

In Excel, open the .txt with File > Open to start the Text Import Wizard, choose Fixed width, and click to place the column breaks. A TXT to CSV converter can also detect the columns from the dashed line under the header.

How do I convert many text files at once?

Use a short script, for example the pandas code on this page in a loop over the files. For a few files, converting them one by one online is quicker.